Cryptology Academy · Lesson

Block Cipher Fundamentals & Padding

Understand block size, PKCS7 padding, and cipher structure.

Lesson 1 of 413 steps

Block Cipher Fundamentals & Padding is a free Cryptology Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, and your progress syncs across the web and the CoddyKit app. The Cryptology Academy course includes 4 lessons in total.

Welcome

Block ciphers encrypt fixed-size blocks. When plaintext doesn't fill a block, we need padding. In this lesson we master block cipher structure, padding schemes, and why they matter.

Block Size Review

AES uses 128-bit (16-byte) blocks. DES used 64-bit (8-byte) blocks. If plaintext is exactly 16 bytes, it's one block. 17 bytes requires two blocks.

Why Padding Is Needed

If plaintext is 20 bytes, the second block contains only 4 bytes. The cipher needs a full 16-byte block. We pad the remaining 12 bytes with a deterministic, reversible scheme.

PKCS#7 Padding

PKCS#7 pads with N bytes, each having value N: - Need 3 bytes → append: 03 03 03 - Need 12 bytes → append: 0C 0C 0C 0C 0C 0C 0C 0C 0C 0C 0C 0C - Need 0 bytes → append full block of 16: 10×16

Always Pad One Full Block

If plaintext is exactly 16 bytes, PKCS#7 adds a full extra block of 0x10×16. This ensures the receiver always knows where padding ends: the last byte value tells how many bytes to strip.

PKCS#7 in Python

def pkcs7_pad(data: bytes, block_size: int = 16) -> bytes: n = block_size - (len(data) % block_size) return data + bytes([n] * n) def pkcs7_unpad(data: bytes) -> bytes: n = data[-1] return data[:-n]

Padding Oracle Attacks

If a server reveals whether padding is valid after decryption (error message or timing), an attacker can decrypt ciphertext byte-by-byte without knowing the key. This broke ASP.NET ViewState in 2010.

Zero Padding & ANSI X.923

Zero padding: pad with 0x00 bytes (ambiguous — original data might end in zeros). ANSI X.923: pad with zeros, last byte = count. ISO 7816-4: first padding byte = 0x80, rest = 0x00. PKCS#7 is unambiguous and preferred.

Bitwise Padding (CTS)

Ciphertext Stealing (CTS) avoids padding entirely: it rearranges the last two blocks to handle partial-block input. Used in disk encryption (Kerberos, some AES implementations).

Block Size Security Impact

With 64-bit blocks (DES, 3DES, Blowfish): birthday collision after 2^32 blocks (~32 GB). Browsers disabled 3DES in TLS after the SWEET32 attack demonstrated this in 2016. AES's 128-bit blocks need 2^64 blocks — practically safe.

Unpadded Modes

AES-CTR and AES-GCM do not require padding: they use the block cipher as a keystream generator and XOR arbitrary-length input. The block boundary is an internal implementation detail.

Quick Check

Using PKCS#7 with 16-byte blocks, what padding is appended to a 13-byte message?

Recap

Block cipher padding is mastered! Next we compare ECB and CBC modes — starting with the famous ECB penguin attack.
Free to start

Learn Cryptology Academy with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
67
Lessons
261

Frequently Asked Questions

Is the “Block Cipher Fundamentals & Padding” lesson free?

Yes — the full text of “Block Cipher Fundamentals & Padding” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO. The Cryptology Academy course includes 4 lessons in total.

What will I learn in “Block Cipher Fundamentals & Padding”?

Understand block size, PKCS7 padding, and cipher structure. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cryptology Academy?

No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Block Cipher Fundamentals & Padding” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cryptology Academy lesson?

Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Block Cipher Fundamentals & Padding
  2. ECB vs CBC: Patterns & IVs
  3. CTR Mode & Stream-Cipher Behavior
  4. GCM & Authenticated Encryption
← Back to Cryptology Academy