Learning Centre / Cryptography / Symmetric Encryption

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.

AES-128/192/256 DES / 3DES ECB / CBC / CTR / GCM Stream Ciphers Padding Oracle

1 What Is Symmetric Encryption?

ALICE 👩 ENCRYPTION AES(plaintext, K) 🔑 Shared Key K Encrypted text (ciphertext) DECRYPTION AES⁻¹(cipher, K) 🔑 Shared Key K BOB 👨 ⚠ Problem: how to exchange key K securely? Solution: key exchange with Diffie-Hellman or RSA (Hybrid Encryption)
Fast
1000x faster than asymmetric encryption. Ideal for large files.
One Key
The same key is used for encryption and decryption.
Key problem
Securely distributing the key is the main problem. Hybrid encryption is the solution.

2 Block vs Stream Ciphers

Block Ciphers

Encrypts data by dividing it into fixed-size blocks (64-bit, 128-bit).

AES 128-bit block, 128/192/256-bit key
DES 64-bit block, 56-bit key (obsolete)
3DES 64-bit block, 168-bit key (slow)
Blowfish 64-bit block, 32–448-bit key
Twofish 128-bit block, 128/192/256-bit key

Stream Ciphers

Encrypts data bit by bit or byte by byte — a pseudorandom key stream with XOR.

RC4 Weak — removed from HTTPS/WEP
ChaCha20 Modern, used in TLS 1.3
Salsa20 Predecessor of ChaCha20, secure

// Stream cipher basis:

ciphertext[i] = plaintext[i] XOR keystream[i]

3 DES and the Feistel Network

DES Feistel Network (16 rounds) 64-bit Input (Plaintext) L₀ (32 bit) R₀ (32 bit) F(R₀, K₁) ⊕ L₁ = R₀ R₁ = L₀⊕F(R₀,K₁) ... (16 rounds repeated) ... Output: 64-bit Ciphertext K₁ (48-bit)

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.

Inside one AES-128 round (10 rounds, 128-bit key) State (4×4 bytes) A0 B1 C2 D3 E4 F5 06 17 28 39 4A 5B 6C 7D 8E 9F SubBytes With S-box non-linear substitution ShiftRows Rows shift left 0,1,2,3 positions MixColumns GF(2⁸) da matrix multiplication AddRoundKey State XOR Round Key (from key schedule) AES-128: 10 rounds | AES-192: 12 rounds | AES-256: 14 rounds

Number of AES Rounds

Version Key Rounds Security
AES-128128 bit10 Sufficient
AES-192192 bit12 Good
AES-256256 bit14 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)

ECB (Weak!) P₁ AES(K) C1 P₂ AES(K) C2 P₃ AES(K) C3 ⚠ Same P → same C (pattern kept!) CBC (Correct) IV ⊕ P₁ AES(K) C1 ⊕ P₂ AES(K) C2 ⊕ P₃ AES(K) C3 ✓ Same P → different C (depends on IV/prev. C) ECB "penguin" problem: Same-pixel areas in the image remain visible after encryption → Never use ECB!
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)?