Search code examples
javacryptographybouncycastle

Bouncy Castle PKCS7 padding


I need help to find what is actually a padding value for this expression in Bouncy Castle java framework. Kinda not sure about the values.

encrypt(bytes, iv, secret, "AES/CBC/PKCS7Padding")

Solution

  • The padding byte used is the same as the number of padding bytes. Thus, if 11 bytes of padding are needed then that padding consists of 11 copies of the byte 0x0b. Padding is always applied, so that if the number of bytes is already a multiple of 16 then 16 bytes of padding are used, the pad byte being 0x10 in that case.

    This padding scheme has the nice feature that, upon decryption, you can examine the last byte of the padded plaintext to determine how many padding bytes there are in total.

    Note that PKCS7 padding is not a java-specific standard but rather a very common padding scheme independent of language.