6. Block Cipher Modes & Initialization Vectors
Electronic Codebook (ECB) Mode Vulnerabilities
Electronic Codebook (ECB) is the simplest encryption mode where each block of plaintext is encrypted independently using the same key. This means that identical blocks of plaintext result in identical blocks of ciphertext.
The primary weakness is the lack of diffusion. Because identical blocks remain identical, patterns in the original data are preserved in the ciphertext. This is famously illustrated by the 'ECB Penguin' scenario, where a bitmap image encrypted with ECB reveals the outlines of the image because pixel patterns are consistent.
Below is a demonstration using OpenSSL to show how identical blocks result in identical cipher output.
echo 'AAAAAAAABBBBBBBB' > input.txt openssl enc -aes-128-ecb -in input.txt -out output.bin -K 00112233445566778899aabbccddeeff hexdump -C output.bin
Cipher Block Chaining (CBC) and Initialization Vectors
CBC mode solves the ECB pattern issue by XORing each plaintext block with the previous ciphertext block before encryption. This creates a dependency chain, ensuring that even identical plaintext blocks result in different ciphertext blocks.
The Initialization Vector (IV) is a random, non-secret value used as the starting block (the previous block) for the first encryption operation. If the same key and same IV are used for two different messages, an attacker can detect if they start with the same data.
Padding is required in CBC because block ciphers operate on fixed-size blocks (e.g., 16 bytes). PKCS#7 is the standard padding method, filling the remaining space with bytes equal to the number of missing bytes.
from Crypto.Cipher import AES from Crypto.Util.Padding import pad key = b'Sixteen byte key' iv = b'Sixteen byte ivs' plaintext = b'Sensitive data that needs padding' cipher = AES.new(key, AES.MODE_CBC, iv) padded_data = pad(plaintext, AES.block_size) ciphertext = cipher.encrypt(padded_data) print(f'Ciphertext hex: {ciphertext.hex()}')
Understanding Padding Oracle Attacks
A Padding Oracle Attack occurs when a system decrypts ciphertext and reveals whether the padding is valid or invalid through error messages or timing differences.
By observing the server's response to modified ciphertexts, an attacker can systematically guess the plaintext byte-by-byte. This exploit relies on the mathematical property that altering the last byte of a previous block affects the decrypted value of the current block.
Prevention involves using Authenticated Encryption (like AES-GCM) which checks for integrity before attempting to decrypt, making oracle responses impossible.
# Example of identifying server behavior via curl
curl -v -X POST -d '{"data": "base64_cipher"}' http://target-api.local/decrypt
# Look for responses like 'Invalid Padding' vs 'Invalid MAC'
Counter (CTR) Mode Operation
CTR mode transforms a block cipher into a stream cipher. It encrypts a successive counter value rather than the plaintext itself. The resulting keystream is then XORed with the plaintext to create the ciphertext.
Because it is a stream cipher, it supports parallel processing and does not require padding. However, reusing a (Nonce, Key) pair results in the catastrophic loss of security, as the keystream becomes predictable.
The table below summarizes the modes discussed:
| Mode | 特性 |
|---|---|
| ECB | No IV, preserves patterns |
| CBC | Requires IV, needs padding |
| CTR | Stream mode, parallelizable, no padding |
from Crypto.Cipher import AES from Crypto.Util import Counter key = b'Sixteen byte key' ctr = Counter.new(128) cipher = AES.new(key, AES.MODE_CTR, counter=ctr) plaintext = b'Message to encrypt' ciphertext = cipher.encrypt(plaintext) print(f'Encrypted: {ciphertext.hex()}')
Knowledge Check
Ready to test your understanding of 6. Block Cipher Modes & Initialization Vectors?