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)

12
  • @blackapps , I do not want base64 string , I want hex format , edited to show the error Commented Feb 9, 2022 at 12:27
  • Well.. NoSuchMethodError. What is it that you wanna know? Commented Feb 9, 2022 at 12:27
  • @blackapps all stackoverflow answers for hex show this function Commented Feb 9, 2022 at 12:28
  • @blackapps required libraries are all added , even used common 1.3 Commented Feb 9, 2022 at 12:29
  • 1
    Did you try the non static method? Commented Feb 9, 2022 at 12:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

your answer with char[] encodeHex lead me to this : stackoverflow.com/questions/9655181/… now it works , Thanks alot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.