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 bad padding exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "bad padding exception " Watch "bad padding exception " New topic
Author

bad padding exception

sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

i am getting javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)




what would be the solution,when i comment initilizeKey() method in decryptValue method it is working.
but in my case i will be calling encryptValue one time and later decryptValue method.not both at a time.
any suggestion please.

Thanks,

Sudheer
SCWCD, SCJP 5
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Your initializeKey() method creates a random key each time it is called. Your encryptValue() method calls initializeKey() and creates a random key to encrypt with. Your decryptValue() method calls initializeKey() and creates another random, most probably different key from that used in the encryption key, to decrypt with. AES must use the same key for encryption and decryption.

The key should be generated once and saved. You can use a password protected Java Keystore for this.

P.S. You do realise that ECB block mode is considered insecure since it allows forgery though splicing. You should change to use one of the feedback modes such as CBC; this will eliminate this particular insecurity.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

hi thanks for the reply,when i use key store i am getting "should be 16 bit padding error".
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

sudheer kiran wrote:hi thanks for the reply,when i use key store i am getting "should be 16 bit padding error".


Then you are using it wrongly! Not being a mind reader I can't help without seeing the code.

As I said in one of your other threads in this forum, you need to do some reading. By doing as you are doing and cobbling together code from random sources found on the Internet you might end up with something that encrypts and decrypts but based on what you have posted so far it will be insecure.

Bye

P.S. Paraphrasing error messages like that helps nobody.
sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

hey James very thanks.i have only got three days for for to complete module so i been looking for quick solutions.
you are right i should go through complete book.

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

sudheer kiran wrote:hey James very thanks.i have only got three days for for to complete module so i been looking for quick solutions.
you are right i should go through complete book.



I find it hard to believe that any lecturer/teacher would give you just 3 days to complete a cryptography assignment starting from zero knowledge without providing appropriate course material.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bad padding exception
 
Similar Threads
error coming while decrypting...
String to SecretKey
String encryption/decryption - 'block not properly padded' error
Digital Signature in PKCS 7 format with base 64 encoding
BadPaddingException using AES