liveBook · Manning

PKCS#7 padding

As explained in Real-World Cryptography

PKCS#7 padding appends bytes whose values equal the number of padding bytes required to reach a block boundary.

Block ciphers need complete blocks. PKCS#7 padding appends bytes whose value equals the number of bytes added, making the result reversible after decryption. Random filler would not reveal where the original message ends. section 4.3

An eight-byte message for AES

With AES's 16-byte block size, an eight-byte plaintext receives eight padding bytes, each containing the value 8. If the plaintext already fills one block, PKCS#7 adds a full block of sixteen bytes, each containing 16. Decryption reads the final byte and removes that many bytes. section 4.3

Figure 4.6 If the plaintext is not a multiple of the block size, it is padded with the length needed to reach a multiple of the block size. In the figure, the plaintext is 8 bytes, so we use 8 more bytes (containing the value 8) to pad the plaintext up to the 16 bytes required for AES.

Visualize how eight bytes of value 8 extend an eight-byte plaintext to AES's 16-byte block boundary. section 4.3

How Real-World Cryptography covers it

How deep it goes
Explains the padding rule, full-block case, reversibility, and unpadding procedure, but not implementation details.
Examples
Uses eight-byte plaintext padded with eight bytes of value 8, plus a full sixteen-byte padding block.
What it assumes
Assumes block-based encryption and AES's 16-byte block size while explaining padding from its basic purpose.
What you can do after
Apply it when preparing and reversing block-cipher plaintext, including messages already aligned to a block.
How it connects
Connects padding with AES, block ciphers, CBC mode, ECB mode, and decryption.

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 Real-World Cryptography. The section links point to the parts of the book it draws on.