Course contentsShow
← Security
Lesson 107 of 3,0523. Symmetric Cryptography Deep DiveFree lesson

Why Padding is Necessary in Block Ciphers

Learn why block ciphers require padding when plaintext doesn't align to block boundaries and the role padding plays in encryption.

Why Padding is Necessary in Block Ciphers

You'll learn why block ciphers need padding when your data doesn't perfectly fit into fixed-size blocks.

The Block Size Problem

Block ciphers like AES operate on fixed-size chunks of data—AES uses 128-bit (16-byte) blocks. Every single block must be exactly this size to be encrypted. But here's the challenge: your plaintext data rarely comes in perfect multiples of 16 bytes.

Imagine you're shipping boxes that must be exactly 16 inches tall. If your item is only 10 inches, you can't ship it as-is—you need to add padding material to fill the remaining 6 inches. Without it, the shipping system (block cipher) simply won't accept your box.

What Happens Without Padding

If your plaintext is 25 bytes and your block cipher requires 16-byte blocks, you have:

  • Block 1: 16 bytes ✓
  • Block 2: 9 bytes ✗ (missing 7 bytes!)

That incomplete second block cannot be encrypted. The cipher algorithm expects exactly 16 bytes of input for its transformations to work correctly. Missing bytes would cause encryption to fail or produce undefined behavior.

How Padding Solves This

Padding schemes (like PKCS#7) add extra bytes to the final block to reach the required size. Those 9 bytes become 16 by adding 7 padding bytes. Now both blocks encrypt properly.

Padding also allows the decryption process to identify and remove the padding afterward, recovering your original 25-byte message intact.

Key Takeaway: Block ciphers require input in exact block-size multiples; padding fills incomplete final blocks so encryption can proceed, and proper padding schemes ensure the original plaintext can be recovered after decryption.