aspose file tools*
The moose likes Java in General and the fly likes javax.crypto.BadPaddingException: Given final block not properly padded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "javax.crypto.BadPaddingException: Given final block not properly padded" Watch "javax.crypto.BadPaddingException: Given final block not properly padded" New topic
Author

javax.crypto.BadPaddingException: Given final block not properly padded

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hi

Wondering if anyone can help me with what appears to be an intermintent problem with decrypting database data...

I am using teh below (snippets) code to encrypt users' private data to store in a database, then decrypt so they can view it.


To encrypt:



I have read around the forums and most people seem to say it's because when converting the bytes to a String the encrypted data becomes compromised, but I can
not see any other way in which I can handle this encrypted data and pass it to the database then back again.

Any suggestions?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41506
    
  53
You shouldn't use "new String(...)" and "String.getBytes()" to perform the conversion - that implies the data is character data, which simply isn't the case. The easiest would probably be to use base-64 to convert a byte[] into a string; the Apache Commons Codec library can do that.


Ping & DNS - my free Android networking tools app
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Cheers for that, the following code seems to be working, although I haven't fully tested it (don't know if I dare incase I get another 'random' exception!)

Shital Joshi
Greenhorn

Joined: Jun 30, 2010
Posts: 9
Ulf Dittmer wrote:You shouldn't use "new String(...)" and "String.getBytes()" to perform the conversion - that implies the data is character data, which simply isn't the case. The easiest would probably be to use base-64 to convert a byte[] into a string; the Apache Commons Codec library can do that.


i tried the same, but the conversion i am not being able to perform, it gives me error that " Access restriction: BASE64Decoder() " , what to do?
i have all jar files even rt.jar and jce.jar also

please help me out, i facing problem in decryption
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javax.crypto.BadPaddingException: Given final block not properly padded