In my app I'm creating a JsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("command", "session.intSessOK");
jsonObject.put("appToken", appToken);
jsonObject.put("aesIv", aesIv2Encrypted);
jsonObject.put("nextToken", nextToken);
putting values in it
byte[] body = Base64.encode(jsonObject.toString().getBytes("UTF-8"), Base64.NO_WRAP);
AesUtil aesUtil = new AesUtil(aesKeyIv.getKey(), aesKeyIv.getInitVector());
String finalBody = aesUtil.encrypt(body);
converting it to base64 and encrypting it
JSONObject jsonObjectFinal = new JSONObject();
jsonObjectFinal.put("dt", finalBody);
finally creating another json object and send the encrypted body as "dt" value
sendInitOk(jsonObjectFinal);
And then:
private void sendInitOk(JSONObject jsonObject) {
apiService.generic(jsonObject).enqueue(new Callback<GenericWrapper>() {
@Override
public void onResponse(Call<GenericWrapper> call, Response<GenericWrapper> response) {
ApiServiceSingleton.getInstance().setHeaders(response.headers());
Timber.d("INIT SESSION OK: " + response.body().toString());
try {
Aes aes = new Aes();
ApiServiceSingleton apiSingleton = ApiServiceSingleton.getInstance();
byte[] dataBytes = Base64.decode(response.body().getDt(), Base64.NO_WRAP);
byte[] data = aes.decrypt(apiSingleton.getIv().getBytes("UTF-8"), apiSingleton.getKey().getBytes(), dataBytes);
String dataString = new String(data);
Timber.d(dataString);
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
@Override
public void onFailure(Call<GenericWrapper> call, Throwable t) {
Timber.e("INIT SESSION OK ERROR" + t.getMessage());
}
});
}
Api service:
@POST("getRequest")
Call<GenericWrapper> generic(@Body JSONObject jsonObject);
GenericWrapper class in kotlin:
class GenericWrapper {
@SerializedName("dt")
@Expose
var dt: String? = null
}
There is no error in client side but I'm not getting a response.
Backend side gets the json value as NameValuePairs = { "dt" = "......."
instead of "dt" = ...
edit: so I used this answer and switched to Gson's JsonObject but this time I'm getting a BadPaddingException. Strange NameValuePairs thing dissepeared thpough