Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AES Encryption .BadPaddingException: Given final block not properly padded

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pedro Neves
Ranch Hand
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot man it was that. I had to specify noPadding. ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic