Symmetric Encryption
AES, DES, 3DES — both encryption and decryption with a single key. Block and stream ciphers, ECB/CBC/CTR/GCM modes, and the internal working mechanism of AES.
1 What Is Symmetric Encryption?
2 Block vs Stream Ciphers
Block Ciphers
Encrypts data by dividing it into fixed-size blocks (64-bit, 128-bit).
Stream Ciphers
Encrypts data bit by bit or byte by byte — a pseudorandom key stream with XOR.
// Stream cipher basis:
ciphertext[i] = plaintext[i] XOR keystream[i]
3 DES and the Feistel Network
DES Characteristics
- 64-bit block size
- 56-bit effective key length (8 parity bits)
- 16 Feistel rounds
- In 1999, EFF "Deep Crack" broke it in 22 hours
- Today it must not be used — brute force is possible
3DES (Triple DES)
C = DES_K3( DES⁻¹_K2( DES_K1(P) ) )
- Applies DES 3 times (Encrypt-Decrypt-Encrypt)
- 112-bit effective security
- 3x slower than AES
- Deprecated by NIST since 2023
4 AES (Advanced Encryption Standard)
AES — accepted as a standard by NIST in 2001, based on the Rijndael algorithm. Today it is the most widely used symmetric encryption algorithm in the world: HTTPS, WiFi, disk encryption, VPN, and more.
Number of AES Rounds
| Version | Key | Rounds | Security |
|---|---|---|---|
| AES-128 | 128 bit | 10 | Sufficient |
| AES-192 | 192 bit | 12 | Good |
| AES-256 | 256 bit | 14 | Best |
What is the AES S-Box?
In the SubBytes stage, each byte is passed through the S-Box (Substitution Box) table. This is non-linear — it provides cryptographic strength.
// S-Box example (hex)
0x00 → 0x63
0x01 → 0x7c
0xff → 0x16
256 values, GF(2⁸) inversion + affine transform
5 Encryption Modes (Modes of Operation)
| Mode | Need IV? | Parallel? | Error propagation | Security | Usage |
|---|---|---|---|---|---|
| ECB | No | Yes | No | WEAK | NEVER use |
| CBC | Yes (random) | Not a cipher | 2 blocks | AVERAGE | TLS 1.2 (old) |
| CTR | Yes (nonce) | Yes | Does not propagate | GOOD | Stream-like |
| GCM | Yes (nonce, 96-bit) | Yes | Auth tag | BEST | TLS 1.3, HTTPS |
6 AES with Python
Important: Never use ECB mode. When using GCM mode, make sure the nonce is used only once — otherwise security is broken.
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
import base64
# ============================================
# AES-CBC (proper use)
# ============================================
def aes_cbc_encrypt(plaintext: bytes, key: bytes) -> tuple:
iv = get_random_bytes(16) # Random IV, new each time!
cipher = AES.new(key, AES.MODE_CBC, iv)
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
return iv, ciphertext
def aes_cbc_decrypt(iv: bytes, ciphertext: bytes, key: bytes) -> bytes:
cipher = AES.new(key, AES.MODE_CBC, iv)
return unpad(cipher.decrypt(ciphertext), AES.block_size)
# ============================================
# AES-GCM (best choice — with authentication)
# ============================================
def aes_gcm_encrypt(plaintext: bytes, key: bytes, aad: bytes = None) -> dict:
nonce = get_random_bytes(12) # 96-bit nonce, only once!
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
if aad:
cipher.update(aad) # Additional Authenticated Data
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
return {'nonce': nonce, 'ciphertext': ciphertext, 'tag': tag}
def aes_gcm_decrypt(data: dict, key: bytes, aad: bytes = None) -> bytes:
cipher = AES.new(key, AES.MODE_GCM, nonce=data['nonce'])
if aad:
cipher.update(aad)
return cipher.decrypt_and_verify(data['ciphertext'], data['tag'])
# Throws ValueError if tag is incorrect (tampered!)
# Example
key = get_random_bytes(32) # AES-256
msg = b"CTF{s3cr3t_m3ss4g3}"
# CBC
iv, enc = aes_cbc_encrypt(msg, key)
dec = aes_cbc_decrypt(iv, enc, key)
print(f"CBC: {dec}") # b"CTF{s3cr3t_m3ss4g3}"
# GCM
enc_data = aes_gcm_encrypt(msg, key, aad=b"header")
print(aes_gcm_decrypt(enc_data, key, aad=b"header"))
# ============================================
# CTF: ECB Oracle attack (Byte-at-a-time)
# ============================================
def detect_ecb(ciphertext: bytes, block_size: int = 16) -> bool:
blocks = [ciphertext[i:i+block_size] for i in range(0, len(ciphertext), block_size)]
return len(blocks) != len(set(blocks)) # Repeated block = ECB
7 CTF Symmetric Cipher Challenges
Common CTF patterns
ECB mode detection
Identical plaintext blocks produce identical ciphertext blocks. Finding a repeated block = ECB.
CBC Bit-Flipping Attack
Changing the IV or the previous ciphertext block affects the next plaintext block.
Padding Oracle
If the server reveals a padding error, CBC can be decrypted. POODLE (2014) used this vulnerability.
CTR Nonce Reuse
If two different texts are encrypted with one nonce: C1⊕C2 = P1⊕P2. XOR cracking is possible.
CBC Bit-Flipping Example
# Goal: set admin=true
# Plaintext: "role=user;admin=false"
def cbc_bitflip(ciphertext, pos, old, new):
# pos = byte to be changed (in previous block)
c = bytearray(ciphertext)
c[pos] ^= ord(old) ^ ord(new)
return bytes(c)
# to make "false" → "true;"
# calculate the 16-byte block, in the previous block
# change the matching position
# Defense: CBC + MAC (Encrypt-then-MAC)
# Or AES-GCM (has authentication)
Test Your Knowledge
1. What is the main weakness of ECB mode?
2. How many encryption rounds are there for AES-256?
3. Which AES mode also provides authentication (integrity check)?