File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Security and the fly likes AES Encryption .BadPaddingException: Given final block not properly padded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "AES Encryption .BadPaddingException: Given final block not properly padded" Watch "AES Encryption .BadPaddingException: Given final block not properly padded" New topic
Author

AES Encryption .BadPaddingException: Given final block not properly padded

Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Hello. I'm experiencing some problems with AES encryption.

i have a method that calls a method from another class that decrypts some bytes.

Till now i have this



Its important to say that, the method that calls for decryption, surrounds the call by an Exception.
When i run it i'v got the output:

Connect To: 192.168.1.126:12345
-106 -55 22 117 -49 65 9 -123 -71 11 109 105 76 -14 100 41 -127 -114 40 -77 125 63 119 -97 26 62 42 -127 -65 98 -57 -12 array to decrypt in the method that calls for decryption
-106 -55 22 117 -49 65 9 -123 -71 11 109 105 76 -14 100 41 -127 -114 40 -77 125 63 119 -97 26 62 42 -127 -65 98 -57 -12 array to decrypt in the method that decrypts

1
2
3javax.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)
at Geral.AES.Decrypt(AES.java:61)
at Sockets.ligação.read(ligação.java:89)
at Geral.RunnableThread.run(RunnableThread.java:80)
at java.lang.Thread.run(Unknown Source)


Can anyone help me with this exception??

thanks in advance
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

If you look at the API documentation of the Cipher.doFinal() method, you'll find when it will throw a BadPaddingException:
if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes

When you retrieve an instance of Cipher for the AES algorithm, try explicitly providing the padding scheme, instead of relying on the default as you do now.
If no padding was used when the plaintext was encypted, you should explicitly specify NoPadding.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Thanks a lot man it was that. I had to specify noPadding. ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AES Encryption .BadPaddingException: Given final block not properly padded