在我的应用程序中,我正在创建一个JsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("command", "session.intSessOK");
jsonObject.put("appToken", appToken);
jsonObject.put("aesIv", aesIv2Encrypted);
jsonObject.put("nextToken", nextToken);
将值放入其中
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);
将其转换为base64并对其进行加密
JSONObject jsonObjectFinal = new JSONObject();
jsonObjectFinal.put("dt", finalBody);
最后创建另一个json对象并将加密的主体发送为“dt”值
sendInitOk(jsonObjectFinal);
然后:
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服务:
@POST("getRequest")
Call<GenericWrapper> generic(@Body JSONObject jsonObject);
kotlin中的GenericWrapper类:
class GenericWrapper {
@SerializedName("dt")
@Expose
var dt: String? = null
}
客户端没有错误,但我没有收到回复。
后端端获取json值为NameValuePairs = { "dt" = "......."
而不是"dt" = ...
编辑:所以我使用this answer并切换到Gson的JsonObject,但这次我得到BadPaddingException。奇怪的NameValuePairs事情分散了