wood burning stoves*
The moose likes Game Development and the fly likes encryption illegal block size problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "encryption illegal block size problem" Watch "encryption illegal block size problem" New topic
Author

encryption illegal block size problem

kourosh parsa
Ranch Hand

Joined: Jan 22, 2012
Posts: 35
    
    1
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
Sheriff

Joined: Sep 28, 2004
Posts: 18554
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18554
    
  40

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

Joined: Jan 22, 2012
Posts: 35
    
    1
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

Joined: Jan 22, 2012
Posts: 35
    
    1
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
Sheriff

Joined: Sep 28, 2004
Posts: 18554
    
  40

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
 
 
subject: encryption illegal block size problem
 
Similar Threads
encoding in java and decoding in perl
AES decryption problem
Encrypt in Java, Decrypt in IBM DataPower
AES Encryption Service
javax.crypto.BadPaddingException