• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

AES decryption problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have provided an API to our clinet, which they use to encrypt the string and they will send the encrypted string as request parameter to our end.
On receiving the string we will decrypt the string before processing further.
But while decrypting we receive the below error.

Client use JDK 1.6 to encrypt and we use JDK 1.5 at our end to decrypt



javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at com.sbi.crypto.aes.EncryptAES.decrypt(EncryptAES.java:84)




Line 84 is byte [] ciphertext=cipher.doFinal(results);

Below is the code to encrypt


Below is the code to decrypt




Kindly help us.
Thanks in advance.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either the base64 encoded ciphertext is getting corrupted in some way or more likely the wrong key is being used for decryption. For test purposes you should dump the bytes of the key after reading them.

P.S. Using the bytes of the key as the IV is not considered good practice.
P.P.S. your code
can be replaced by the single line

P.P.P.S. That exception handling is poor. If you fail to read the key then it is fatal and you cannot continue!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic