Search code examples
androidkotlinmvvmretrofit

Where i put decryption code for responce in retrofit andriod


I want to decrypt server data where can I decrypt it in android using retrofit

before using enc/dec I get this data from server

{
  "success": true,
  "secret": "NVARWBA4MAGSAW2F"
}

When I hit this API

@Headers("Content-Type: application/json")
    @POST("user")
    suspend fun addUser(@Body addUser: AddUser): Response<com.pryze.repository.model.User>

and after using enc/dec in response from server only I get encrypted text

'JAdS9hy168A2fG6FVTyzmFY739iawyk9qZ/yynRLtFTtE9nXxHyEas5ZrLzpl9IhpdgD27RpPBS5HsFHnVParg=='

and my app is crushed due to illegal response how can I solve this where I can put my dec code to first decrypt it then assign that to the response.


Solution

  • please try this code in your inspector class for Encryption and Decryption both.

    override fun intercept(chain: Interceptor.Chain): Response {
            if (!isInternetAvailable()){
                 throw NoInternetException("Make sure you have an active data connection")
            }
                try {
                    val aesUtil = AesUtil(keySize, iterationCount)
            var request: Request = chain.request()
                    val buffer = Buffer()
                    request.body?.writeTo(buffer)
                    val strOldBody: String = buffer.readUtf8()
            val encText = aesUtil.encrypt(salt,iv,passphrase,strOldBody)
                    val mediaType: MediaType? = "text/plain; charset=utf-8".toMediaTypeOrNull()
                    val strNewBody: String = encText
                    val body: RequestBody = RequestBody.create(mediaType, strNewBody)
                    request = request.newBuilder().addHeader("authorization", "Bearer " + t.getToken("JWT")).header("Content-Length", body.contentLength().toString()).method(request.method, body).build()
                    var req= chain.proceed(request)
                    var enc_data=""+req.body?.string().toString()
            enc_data = aesUtil.decrypt(salt,iv,passphrase,enc_data)
            return req.newBuilder().body(ResponseBody.create(req.body?.contentType(), enc_data)).build()
                }
                catch (e:ServiceConfigurationError){
                 }
                catch (e: SSLHandshakeException){
                    throw NoInternetException("Request Time Out")
                }
                catch (e: ConnectException){
                    throw NoInternetException("Request Time Out")
                }
                catch (e: SocketTimeoutException){
                    throw NoInternetException("Make sure you have an active data connection")
                }
                catch (e:UnknownHostException){
                    throw NoInternetException("Make sure you have an active data connection")
                }
                catch (e: ErrnoException){
                    throw NoInternetException("Request Time Out")
                }
            throw NoInternetException("Request Time Out")
        }