1
$\begingroup$

I am using the javax.crypto.Cipher in Java to do AES/CBC/PKCS5Padding encryption. What I am observing is that, if you try to encrypt a Plaintext with no padding, PKCS5 adds another entire block just to padding. For example, if I got a 32bytes Plaintext, assuming that AES is using 16byte block size, the padding size should be 0. But, instead of that, PKCS5 adds another block for padding.

Is this correct or is just a matter of javax.crypto.Cipher implementation?

$\endgroup$
1
$\begingroup$

Is this correct or is just a matter of javax.crypto.Cipher implementation?

It is correct. You need to always add some amount of padding if you are to distinguish between different padding lengths. With PKCS5 padding the length of the padding is $b - |m| \mod b$, where $b$ is the block size. With $|m|$ a multiple of block size a whole block of padding is added.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.