liveBook · Manning

PKCS#7 padding

As explained in Hacking Cryptography

PKCS#7 padding is a byte-padding scheme in which each padding byte contains the number of padding bytes added to complete a block.

PKCS#7 padding makes plaintext fit a block cipher by appending repeated bytes whose value equals the number of bytes added. For example, four added bytes become 0x04 0x04 0x04 0x04. When plaintext already fills the final block, an entire block of padding is appended. section 5.2

Padding removal uses the final byte to determine how many bytes to remove. Validation must first confirm that this value is positive, within the data length, and repeated across every padding byte; otherwise, malformed ciphertext can be accepted or produce misleading results. section 5.4.1

A safe implementation follows these rules:

Padding oracle exposure

A server encrypts timestamps with AES-CBC and later decrypts submitted ciphertext. If invalid padding produces an identifiable error before timestamp parsing, the response reveals whether the decrypted plaintext has valid PKCS#7 padding. That distinction creates a padding oracle and can let an attacker decrypt the message without learning the key. section 5.4.1

How Hacking Cryptography covers it

How deep it goes
Defines the byte rule, exact-block behavior, construction, removal, validation, and padding-oracle consequence.
Examples
Uses repeated 0x04 bytes, Go padding functions, and an AES-CBC encrypted-timestamp server.
What it assumes
Assumes block-cipher terminology and byte arrays while explaining block alignment and padding behavior directly.
What you can do after
Build it from scratch with padding, removal, and validation routines while avoiding distinguishable padding errors.
How it connects
Connects block ciphers, CBC mode, AES, padding validation, and padding oracle attacks.

Also explained in

Compare how 2 books explain PKCS#7 padding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.