Lesson 5 of 15
Block Cipher Modes
Block Cipher Modes of Operation
A block cipher encrypts fixed-size blocks of plaintext. The same key encrypts each block, but modes of operation define how blocks are chained together. The choice of mode dramatically affects security.
ECB — Electronic Codebook
The simplest mode: each block is encrypted independently.
Fatal flaw: identical plaintext blocks produce identical ciphertext blocks. The famous "ECB penguin" — encrypting a bitmap in ECB mode reveals the image structure.
CBC — Cipher Block Chaining
Each block is XORed with the previous ciphertext block before encryption:
The first block uses an Initialization Vector (IV):
Decryption:
CBC eliminates the ECB pattern flaw. Each ciphertext block depends on all previous plaintext blocks.
Our Simulation
We simulate the block cipher with simple XOR: .
- ECB:
- CBC encrypt: , starting with
- CBC decrypt:
Your Task
Implement:
ecb_encrypt(blocks, key)— XOR each block with keycbc_encrypt(blocks, key, iv)— CBC encryptioncbc_decrypt(blocks, key, iv)— CBC decryption
Python runtime loading...
Loading...
Click "Run" to execute your code.