1

In our android app, we were encrypt and decrypt the username and password by using cipher algorithm [AES/CBC/PKCS7Padding].

We are getting javax.crypto.BadPaddingException on some of the devices. I am sure we are passing same key for both encryption and decryption. Can anybody have any idea about this or facing this issue?

Please note that I can't able to place the code here and I am bit sure passing same key for both encryption and decryption.

AnKr
  • 433
  • 1
  • 6
  • 20

2 Answers2

1

In the Java world, there is no PKCS7Padding. PKCS7Padding is extendedly called "PKCS5Padding". Maybe Samsung's implementation is strictly obeying to this convention. Therefore it won't recognize the PKCS7 padding. If you'd like to make your code work on all devices (including those Samsungs), specify "AES/CBC/PKCS5Padding" instead. Even with that, the same result will be obtained.

See also the list of Cipher transformations officially supported by Android.

ardget
  • 2,561
  • 1
  • 5
  • 4
0

Are you converting the encrypted bytes to hex string and hex string back to bytes for decryption?

Please refer - javax.crypto.BadPaddingException

Kotlin version of the above answer ->

fun fromHexString(s: String): ByteArray {
        val len: Int = s.length
        val data = ByteArray(len / 2)
        
        var i = 0
        while (i < len) {
            data[i / 2] = ( (Character.digit(s[i], 16) shl 4) + Character.digit(s[i + 1], 16) ).toByte()
            i += 2
        }
        
        return data
    }
Sasaki
  • 118
  • 9