• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AES Encryption .BadPaddingException: Given final block not properly padded

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot man it was that. I had to specify noPadding. ;)
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For future readers, BadPaddingException usually indicates that the wrong key is used for decryption.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic