Padding
Padding is the mechanism that extends the plaintext to a block boundary; common schemes include PKCS#7, zero padding, and ISO 10126, while AEAD modes need none.
Purpose
It lets block ciphers process plaintexts whose length is not a multiple of the block size.
Mechanism
Bytes are appended to the plaintext so the total length becomes a multiple of the block size, and are stripped on decryption. PKCS#7 appends n bytes each of value n (reversible and self-verifying). Zero padding is simple but cannot distinguish padding from data. Stream modes (CTR) and AEAD (GCM) need no padding.
Use cases
- AES-CBC/PKCS#7 padding
- RSA OAEP padding
- Legacy zero-padding schemes
- Teaching examples
Examples
- Input
3 bytes + PKCS#7 (block=8)Outputappends 05 05 05 05 05Pads with five 0x05 bytes
Common errors
- Zero padding cannot distinguish trailing data zeros from padding
- Use a self-describing scheme such as PKCS#7
- Padding errors fall to Padding Oracle attacks
- Use AEAD or constant-time validation
Related terms
PKCS#7 Padding
PKCS#7 padding appends n bytes of value n (1 ≤ n ≤ block_size) to the end of the plaintext; reversible and self-verifying, it is the de facto standard padding for CBC mode. GLOSSARY
Block Cipher
A block cipher is a symmetric cipher that encrypts and decrypts fixed-length blocks (e.g., 128 bits) and requires a mode of operation to handle arbitrary-length data. GLOSSARY
CBC (Cipher Block Chaining)
CBC mode XORs the previous ciphertext block with the current plaintext before encrypting, so identical plaintext blocks yield different ciphertexts; it requires a random IV but provides no integrity authentication. GLOSSARY