I have a problem with the decryption error body from the response using Retrofit. I can get the error body from retrofit below:
val json = JSONObject(res.errorBody?.string())
val data = json.get("data").toString()
Noted: ErrorBody
{"data":"bsV9RBdM4GIDX6jvjvYdsUpkRMtl6JzhzOFiucZ+r78sbr3Tucjq9Idq7wL9qKTfDMPXpXcmubIKFzQzbomMNYoAlA9BTgdyA/6lkJQjAOk="}
After that, I need to decrypt object data to real data. Below is my encryption:
fun decrypt(encrypted: String?, iv: String, key: String): String {
return try {
val sKeySpec = generateKey(key)
val ivKey = generateIV(iv)
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, ivKey)
val original = cipher.doFinal(Base64.decode(encrypted, Base64.NO_WRAP))
return String(original, Charsets.UTF_8)
} catch (e: Exception) {
Timber.e("Error Decrypt: $e")
""
}
}
Call encryption
val decryption = EncryptionUtils.decrypt(data, iv, key)
Timber.e("oooo: ${decryption}")
So when I log decryption, It shows like below:
ApiError(code=401, message=Unauthorized, data=null, errors=null)
But real data response from the server like below:
{"code" : 401, "message" : "Unauthorized",.....}
I didn't know what's the problem. Everyone, please help to resolve me that why it's return like this
ApiError(code=401, message=Unauthorized, data=null, errors=null)
I want it to return like below:
{"code" : 401, "message" : "Unauthorized",.....}