This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

encryption illegal block size problem

 
kourosh parsa
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys.
[Here is my improved question regarding encryption]
I'm getting stuck with my encryption.
I'm getting this error:
javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes. I do NOT know why it's expecting 16 bytes and I do not know why we are expected to bring it to base 64...
so I tried filling the bytes up to be multiple of 16 but then after encoding and decoding, I get extra characters at the end.
here is my encryption:


Can someone explain to me why I need a multiple of 16 and how would I fix this please?
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kourosh parsa wrote:Hi guys.
I'm getting stuck with my decryption.
I'm getting this error:
javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes
and the reason is that my message length is 45 bytes. so I used two different approaches to correct the size but both are converting the length to 60 bytes instead of 64. (64 is a multiple of 16)

Method 1:
[DELETED]

Method2 using the package: org.apache.commons.codec.binary.BaseNCodec
[DELETED]

any idea why BASE64 encoders give 60 bytes instead of 64?


And since the OP seemed to have also deleted/changed the original post above -- here is the original code that I removed from my response... for context purposes.







Question: Do you know what a BASE64 encoder is supposed to do??? .... Hint. The "64" part of the name has nothing to do with the resultant size.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kourosh parsa wrote:Hi guys.
[Here is my improved question regarding encryption]
I'm getting stuck with my encryption.
I'm getting this error:
javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes. I do NOT know why it's expecting 16 bytes and I do not know why we are expected to bring it to base 64...
so I tried filling the bytes up to be multiple of 16 but then after encoding and decoding, I get extra characters at the end.
here is my encryption:


Can someone explain to me why I need a multiple of 16 and how would I fix this please?



You really shouldn't modify previous posts like that -- after all, that is what the "reply" button is for.... by editing previous posts, nobody can really follow the topic, and your response rates drops dramatically.


kourosh parsa wrote:
javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes. I do NOT know why it's expecting 16 bytes and I do not know why we are expected to bring it to base 64...
so I tried filling the bytes up to be multiple of 16 but then after encoding and decoding, I get extra characters at the end.

Can someone explain to me why I need a multiple of 16 and how would I fix this please?


Simply. You requested "no padding", so you are responsible for making sure that the padding is correct. This means that you are responsible for ensuring the size is correct -- meaning filling it out to the correct size *and* removing the extra characters.

Henry
 
kourosh parsa
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the comments.
The cipher padding setting does not fix the issue. I changed the padding of encryption and decryption both to PKCS5Padding and still ended up with funny characters at the end of the string.
After observing this example:
https://gist.github.com/bricef/2436364
I realize that my encryption is ok and I just need to remove the null end of string characters in decryptor. What makes no sense is that I have been using my java decryption along with a php encryption both with no padding (because php padding does not seem to be compatible) and yet I never ended up with null characters at the end. I guess that could have occurred by accident.

Nevertheless, if someone thinks that I should have tried another padding or I made some mistake, please let me know because it feels strange not to have a more straight forward way without having to deal with padding and the length.
 
kourosh parsa
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I lied. It works fine with PKCS5Padding. I just forgot to remove my own padding.
Sorry, my questions were silly indicating that I need a break. No more staying up till 4 am.
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kourosh parsa wrote:Actually I lied. It works fine with PKCS5Padding. I just forgot to remove my own padding.
Sorry, my questions were silly indicating that I need a break. No more staying up till 4 am.


And thank you for not overwriting your previous post with this one. I am sure other ranchers would appreciate what you tried out (and your thinking process) as you solved this.

Marking this topic as resolved.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic