0

I am using node forge package to encrypt and decrypt on my server , now I am trying to encrypt data from my client app on android and want to send data back to my server in hex format but the below code is not working

val KEY_AES= "************"
val IV_AES= "************"
fun encrypat(value: String): String? {
    try {
        val key: ByteArray = KEY_AES.toByteArray()
        val ivs: ByteArray = IV_AES.toByteArray()
        val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
        val secretKeySpec = SecretKeySpec(key, "AES")
        val paramSpec: AlgorithmParameterSpec = IvParameterSpec(ivs)
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, paramSpec)
        return Hex.encodeHexString(cipher.doFinal(value.toByteArray(charset("UTF-8"))),true)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

error :java.lang.NoSuchMethodError: No static method encodeHexString([BZ)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Hex; or its super classes (declaration of 'org.apache.commons.codec.binary.Hex' appears in /system/framework/org.apache.http.legacy.jar)

LumbusterTick
  • 1,067
  • 10
  • 21

1 Answers1

1

You are using the legacy version of apache.http. But you should be able to get the same results with the available char[] encodeHex(byte[] data) method.

Jonas
  • 159
  • 8
  • your answer with char[] encodeHex lead me to this : https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java?page=1&tab=votes#tab-top now it works , Thanks alot – LumbusterTick Feb 09 '22 at 12:59