Skip to main content
Symmetric Encryption: AES, Block Modes & AEAD
Lesson 5 of 19

Symmetric Encryption: AES, Block Modes & AEAD

Explain how AES and ChaCha20 work, choose safe authenticated modes like AES-GCM, and prevent ECB, CBC bit-flipping, and nonce-reuse vulnerabilities.

Intermediate 30 min Cryptography

Learning objectives

  • Explain why symmetric ciphers are paired with asymmetric key exchange in hybrid encryption
  • Describe the four operations inside an AES round and compare block ciphers with stream ciphers
  • Compare ECB, CBC, CTR, and GCM modes and justify why AEAD is required in modern systems
  • Identify and mitigate ECB pattern leakage, CBC bit-flipping, padding oracles, and nonce reuse

Recommended first: Classical Ciphers: Caesar, Vigenere, XOR & Frequency Analysis

#Why this matters

Whenever you open an HTTPS site, connect to a VPN, or encrypt a disk, a symmetric cipher does the bulk work. Symmetric encryption uses one shared secret key K for both encryption and decryption, running roughly 1000 times faster than RSA.

The main challenge is distributing K safely over an untrusted network. TLS 1.3 solves this with hybrid encryption: asymmetric cryptography (X25519 or RSA) negotiates a session key, and a fast symmetric AEAD cipher (AES-GCM or ChaCha20-Poly1305) encrypts the traffic.

#Block ciphers, stream ciphers, and legacy algorithms

Block ciphers transform fixed-size blocks (128 bits for AES) using a mode of operation. Stream ciphers derive a pseudorandom keystream from a key and nonce and XOR it with plaintext: ciphertext[i] = plaintext[i] XOR keystream[i].

Legacy ciphers still appear in older systems, but they are obsolete and insecure in 2026:

Cipher Type Block / Key 2026 Status
DES Block (Feistel, 16 rounds) 64-bit / 56-bit Broken by brute force; never use
3DES Block (3x DES) 64-bit / 112-bit eff. Deprecated by NIST after 2023; Sweet32 risk
RC4 Stream Variable Broken keystream biases; banned in TLS
AES Block (SPN) 128-bit / 128, 192, 256 Current standard; use with GCM
ChaCha20 Stream 256-bit key, 96-bit nonce TLS 1.3 standard with Poly1305

DES split 64-bit blocks into 32-bit L and R halves inside a Feistel network, but its 56-bit key falls to brute force. 3DES kept the 64-bit block, exposing long sessions to Sweet32 birthday collisions. RC4 was removed from HTTPS and WEP. Use AES-256-GCM, AES-128-GCM, or ChaCha20-Poly1305 instead.

#Inside AES

AES (NIST FIPS 197) operates on a 128-bit block arranged as a 4x4 byte state. AES-128 runs 10 rounds, AES-192 runs 12 rounds, and AES-256 runs 14 rounds.

  1. SubBytesNon-linear byte substitution via the S-Box
  2. ShiftRowsShift rows 0, 1, 2, 3 left by 0, 1, 2, 3 bytes
  3. MixColumnsMatrix multiplication in GF(2^8) (skipped in final round)
  4. AddRoundKeyXOR the 4x4 state with the round key

SubBytes supplies confusion using a 256-byte S-Box built from GF(2^8) inversion and an affine map. ShiftRows and MixColumns spread single-bit changes across all 16 bytes within two rounds, while AddRoundKey mixes in the key schedule. Hardware AES-NI instructions execute these steps in constant time.

#Modes of operation and AEAD

A raw block cipher only handles 16 bytes. Modes of operation define how to encrypt full messages:

Mode IV / Nonce Padding Integrity Recommendation
ECB None Yes No Never use; leaks patterns
CBC Random 16-byte IV Yes (PKCS#7) No Legacy only; needs Encrypt-then-MAC
CTR Unique nonce No No Never alone; needs a MAC
GCM Unique 96-bit nonce No 128-bit tag Recommended AEAD default
Unauthenticated ECB/CBC vs Modern AEAD
ECB / Unauthenticated CBC
  • ECB exposes repeated plaintext blocks
  • CBC without MAC allows bit-flipping and padding oracles
  • Cannot protect plaintext headers
AES-GCM / ChaCha20-Poly1305
  • Unique nonce hides all block patterns
  • Built-in tag rejects any modified ciphertext
  • Authenticates headers via AAD

Encrypting two identical 16-byte blocks in OpenSSL immediately exposes ECB pattern leakage:

Comparing AES-128-ECB and AES-128-CBC on repeated blocks
$ printf 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' | openssl enc -aes-128-ecb -K 00112233445566778899aabbccddeeff -nopad | python3 -c "import sys; h=sys.stdin.buffer.read().hex(); print(h[:32]); print(h[32:])"
5301d1251af08a9ea49cf85982d8df6e
5301d1251af08a9ea49cf85982d8df6e
$ printf 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' | openssl enc -aes-128-cbc -K 00112233445566778899aabbccddeeff -iv 11223344556677889900aabbccddeeff -nopad | python3 -c "import sys; h=sys.stdin.buffer.read().hex(); print(h[:32]); print(h[32:])"
e33e2bb26705810a0ce2042a02b8e157
94c56406fa164c8fe363e9e110c4af6a

In ECB, both blocks match (5301d125...), revealing structure. In CBC, chaining hides repetition, but CBC still lacks authentication.

In Python, prefer AES-GCM (AEAD). It combines CTR encryption with a GHASH tag and optional Additional Authenticated Data (aad), requiring no block padding:

python
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

def aes_gcm_encrypt(plaintext: bytes, key: bytes, aad: bytes = None) -> dict:
    nonce = get_random_bytes(12)  # 96-bit nonce
    cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
    if aad:
        cipher.update(aad)
    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"])

Tampering with ciphertext, tag, or aad causes decrypt_and_verify to raise ValueError.

#Defending / Common mistakes

  • Using ECB mode. Repeated 16-byte blocks produce identical ciphertexts; replace ECB with AES-GCM or ChaCha20-Poly1305.
  • Unauthenticated CBC or CTR. Without a MAC, CBC allows bit-flipping (c[pos] ^= ord(old) ^ ord(new)) and padding oracles. Use AEAD, or pair legacy CBC with Encrypt-then-MAC.
  • Reusing a CTR or GCM nonce. Same key and nonce yield C1 XOR C2 = P1 XOR P2, and in GCM also leak the GHASH authentication key. Always generate a unique 96-bit nonce per message.
  • Retaining DES, 3DES, or RC4. Disable all 64-bit block ciphers and RC4 in TLS and storage configs.

#Summary

  • Symmetric encryption shares one secret key and runs ~1000x faster than asymmetric crypto, so protocols pair them in hybrid encryption.
  • AES runs SubBytes, ShiftRows, MixColumns, and AddRoundKey on a 4x4 state for 10, 12, or 14 rounds; DES, 3DES, and RC4 are obsolete.
  • Default to AEAD modes (AES-GCM with unique 96-bit nonces or ChaCha20-Poly1305) rather than ECB or unauthenticated CBC/CTR.
  • Bind unencrypted metadata in aad and always verify the authentication tag before trusting decrypted data.

Lecture presentations

Slides on this topic: view them in the browser or download the PDF.

Symmetric Block Encryption: Feistel Network and DES Algorithm

From diffusion and confusion to the round function and S-boxes

Slides: 26 PDF · 1.0 MB
AES Algorithm: Structure, Round Functions, S-box and MixColumns

From the state matrix to the Galois field — the inner workings of an SP network

Slides: 26 PDF · 921 KB
Block Cipher Modes of Operation

From a single block to data of unlimited length: ECB, CBC, CFB, OFB and CTR

Slides: 24 PDF · 829 KB

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge