0Pricing
Cryptology Academy · Lesson

ECB vs CBC: Patterns & IVs

Demonstrate the ECB penguin attack and why CBC with a random IV is required.

ECB vs CBC: Patterns & IVs is a free Cryptology Academy lesson on CoddyKit — lesson 2 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

ECB and CBC are two block cipher modes. ECB is famously insecure due to pattern leakage. CBC with a random IV is much stronger. Let's understand both deeply.

ECB Mode: Electronic Codebook

ECB encrypts each block independently: Ci = AES(K, Pi). Same plaintext block → same ciphertext block every time. Blocks in one message or across messages are identical if content matches.

The ECB Penguin

The 'ECB penguin' is a classic demonstration: encrypt a PNG of Tux the Linux penguin with AES-ECB. The output still looks like the penguin — same colored areas encrypt to the same patterns.

ECB Vulnerability: Block Replay

In ECB mode, an attacker can rearrange ciphertext blocks without knowing the key: - Blocks are independent - Reorder blocks → reorder decrypted plaintext - Duplicate blocks → duplicate content - Delete blocks → drop content

CBC Mode: Cipher Block Chaining

CBC XORs each plaintext block with the previous ciphertext before encrypting: C0 = AES(K, P0 XOR IV) Ci = AES(K, Pi XOR C(i-1)) Same plaintext in different positions → different ciphertext.

Initialization Vector (IV)

The IV is the 'C(-1)' for the first block. It must be: - Random and unique per message - Not secret (transmitted with ciphertext) - Never reused with the same key A predictable IV breaks CBC security.

CBC Decryption

Pi = AES_Dec(K, Ci) XOR C(i-1) P0 = AES_Dec(K, C0) XOR IV Note: decrypting block i only needs Ci and C(i-1). Blocks can be decrypted in parallel (unlike encryption).

CBC Bit-Flipping Attack

Flipping bit i in block Ci causes: (1) garbled Pi, (2) predictable flip in bit i of P(i+1). Without authentication, an attacker can modify specific plaintext bits while knowing where they are.

BEAST Attack on TLS 1.0 (2011)

BEAST exploited TLS 1.0's use of CBC with predictable IVs. The last block of one record was used as the IV for the next. Demonstrated by Rizzo and Duong, forcing browser upgrades to TLS 1.1+.

Why CBC Requires Authentication

CBC encryption without a MAC allows bit-flipping attacks and padding oracle attacks. Always combine CBC with HMAC (Encrypt-then-MAC) or switch to AES-GCM (AEAD built-in).

Practical CBC in Python

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os iv = os.urandom(16) key = os.urandom(32) cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) enc = cipher.encryptor() ciphertext = enc.update(padded_plaintext) + enc.finalize()

Quick Check

Why does ECB mode leak information about plaintext structure?

Recap

ECB is dangerous, CBC is better but needs authentication. Next we study CTR mode — turning AES into a stream cipher.

Frequently asked questions

Is the “ECB vs CBC: Patterns & IVs” lesson free?

Yes — the full text of “ECB vs CBC: Patterns & IVs” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. 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.

What will I learn in “ECB vs CBC: Patterns & IVs”?

Demonstrate the ECB penguin attack and why CBC with a random IV is required. 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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ECB vs CBC: Patterns & IVs” 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