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 javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8" Watch "javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8" New topic
Author

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8

Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
Hi,
I'm using "DES/ECB/PKCS5Padding" getting the following error when decrypting my encrypted value.

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

It fails on the line "byte[] ciphertext = cipher.doFinal( cleartext );". Any ideas what I'm missing ?




Thanks In Advance
Jason
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
After some testing I determined that if I go beyond 56 characters that I am encrypting then decrypting , that is where it fails ( on the decrypt ) . Does this have something to do with DES being 56bit ?
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
I think I may have figured it out. When writing the encrypted value to a file if it's too long it is putting a line break/carriage return into the string and then when it tries to decrypt the line ( which is now stored as 2 lines ) it doesn't match and thus won't work.

Can I prevent the carriage return / line feeds ?
greg stark
Ranch Hand

Joined: Aug 10, 2006
Posts: 220
The code you showed doesn't write or read anything, so there's no way of knowing! It sounds like it is an issue with your base64 encoder/decoder.


Nice to meet you.
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
Here is the code that is doing the writing to the file. Hopefully someone can notice something I have missed which would cause line breaks within the encrypted text.





Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4655
    
    5

Jason Wylde wrote:Can I prevent the carriage return / line feeds ?

Don't use a Base64 decoder. The standard specs for it say that it wraps lines. Or, make sure you filter out the whitespace properly both in and out.

Also, the algorithms are defined on arrays of unsigned octets. This is not at all the same as a Java String.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8