I have to encrypt data using a key received from a service. The function below is written as per instruction given from supplier.
It crashes and giving javax.crypto.BadPaddingException: Given final block not properly padded.
Anyone kindly advise how to resolve same.
Thanking you in advance.
RECIEVED KEY: uj0rdXLW90EaoD5kMcz8ydYy0ICQGSbYCP26yDpq0BQFZCMK9cn+2plig1P2RYV9
Error occurred on line: 223 (B4XMainPage)
javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
It crashes and giving javax.crypto.BadPaddingException: Given final block not properly padded.
Anyone kindly advise how to resolve same.
Thanking you in advance.
RECIEVED KEY: uj0rdXLW90EaoD5kMcz8ydYy0ICQGSbYCP26yDpq0BQFZCMK9cn+2plig1P2RYV9
EncryptDataUsingKey:
Sub EncryptDataUsingKey (keyReceived As String, OurData As String) As String
Dim Cipher As Cipher
Dim kg As KeyGenerator
Dim md As MessageDigest
Log ("RECIEVED KEY: " & keyReceived)
Cipher.Initialize("AES/ECB/PKCS5PADDING")
kg.Initialize("AES")
kg.GenerateKey
'DECRYPT KEY keyReceived USING RANDOM GENERATED KEY===========
Dim DecrytedKey() As Byte = Cipher.Decrypt(su.DecodeBase64(keyReceived),kg.Key , False)
kg.KeyFromBytes(md.GetMessageDigest(DecrytedKey ,"MD5") )
'ENCRYPT DATA USING KEY FROM DecrytedKey =====================
Dim NewData As String = su.EncodeBase64(Cipher.Encrypt(OurData.GetBytes("UTF8") ,kg.Key,False ))
Log ("RANDOM KEY: " & kg.Key)
Return NewData
End Sub
Error occurred on line: 223 (B4XMainPage)
javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)