This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Security and the fly likes javax.crypto.BadPaddingException for AES when encrypting and decrypting multiple times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "javax.crypto.BadPaddingException for AES when encrypting and decrypting multiple times" Watch "javax.crypto.BadPaddingException for AES when encrypting and decrypting multiple times" New topic
Author

javax.crypto.BadPaddingException for AES when encrypting and decrypting multiple times

Mridul Dinakar
Greenhorn

Joined: Feb 22, 2012
Posts: 3
Hi,

I am trying to encrypt and decrypt a message(String) using a given key string. For that, two seperate methods (one for encrypt and the other for decrypt) were called and when these methods are called from my applicaion. I am getting the below exception while decrypting. The encryption and decryption operations are working properly for the first time.Again if I call the methods with different message and key value it is throwing the below exception.

Exception:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
at javax.crypto.Cipher.doFinal(Cipher.java:2086)

I don't think that, there is a problem with data. Because using the same data (key and message) when I try to call the encrypt/decrypt methods it is working properly. Also if I call the methods from my application for the first time, it will work fine. Please help me to solve the issue......
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Impossible to say without seeing the encryption/decryption code.
Mridul Dinakar
Greenhorn

Joined: Feb 22, 2012
Posts: 3
Tim Moores wrote:Impossible to say without seeing the encryption/decryption code.


Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Looks complicated. Any particular reason you're using the asHex and hexStringToByteArray methods instead of somethingmore standard like base-64 encoding? Lots of tested libraries are available for that, so you'd know the problem isn't with that part.
 
jQuery in Action, 2nd edition
 
subject: javax.crypto.BadPaddingException for AES when encrypting and decrypting multiple times
 
Similar Threads
javax.crypto.BadPaddingException: Given final block not properly padded
BadPaddingException with DES
Encrypt in Java, Decrypt in IBM DataPower
javax.crypto.BadPaddingException when trying to run AES enryption and decryption multiple times
BadpaddingException