Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jason Wylde
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Wylde
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic