Monday, July 13, 2009

Calculate Cipher Size

Calculating Cipher Size for Symmetric algo.:
If you are using "DES/CBC/PKCS5Padding" cipher instance, and using cipher api like:
int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
you may want to think about what cipher text size calculation should be used for this. Because the output byte require a fixed length before encrypt/decrypt.

Solution: If plain text size is x and Encryption block size is y then

Size of CipherText = x + y - (x mod y)

The resulting ciphertext size is computed as the size of the plaintext extended to the next block. If padding is used and the size of the plaintext is an exact multiple of the block size, one extra block containing padding information will be added.

Source: http://www.obviex.com/Articles/CiphertextSize.aspx