Symmetric Encryption

Shared-key encryption — the one-time pad and XOR, stream ciphers vs block ciphers, confusion and diffusion, DES and why it fell, AES rounds and key sizes, ChaCha20, hardware acceleration, and key management basics.

Intermediate 4 min readLesson 3 of 12

In symmetric encryption, the same secret key encrypts and decrypts:

C=EK(P),P=DK(C)C = E_K(P), \qquad P = D_K(C)

It's fast enough to encrypt disks, network traffic and databases at line rate. The hard part is getting the key to both parties securely, which public-key cryptography solves.

XOR and the one-time pad

XOR (⊕) is the basic tool: a⊕k⊕k=aa \oplus k \oplus k = a. The one-time pad XORs the message with a truly random key of the same length:

P  = 01001000 01101001        ("Hi")
K  = 11010011 00111010        (random, used once)
C  = 10011011 01010011        P ⊕ K
C ⊕ K = 01001000 01101001     back to "Hi"

It's perfectly secret (Shannon, 1949): every plaintext of that length is equally likely given the ciphertext. But the key must be as long as all the data, truly random, and never reused. Reuse it once and C1⊕C2=P1⊕P2C_1 \oplus C_2 = P_1 \oplus P_2 — the key cancels out, and the two plaintexts can often be recovered (as the Venona project did to Soviet messages).

Practical ciphers replace the giant random key with a short key that drives a pseudo-random process.

Stream ciphers

A stream cipher expands a short key (plus a nonce) into a long pseudo-random keystream, XORed with the plaintext — a practical one-time pad.

  • ChaCha20 (Daniel Bernstein): 256-bit key, 96-bit nonce; based on simple add–rotate–XOR operations; fast in software on any CPU and naturally resistant to timing attacks. Used in TLS, SSH, WireGuard and on mobile devices without AES hardware.
  • RC4: once ubiquitous (WEP, early TLS), now broken — biased keystream bytes.

The same rule as the one-time pad applies: never reuse a (key, nonce) pair, or the keystream repeats and C1⊕C2=P1⊕P2C_1 \oplus C_2 = P_1 \oplus P_2.

Block ciphers

A block cipher is a keyed permutation on fixed-size blocks: AES maps a 128-bit block to a 128-bit block under a key. To encrypt longer messages, you use it in a mode of operation (Block cipher modes) — and modern modes like CTR and GCM actually turn the block cipher into a stream cipher.

Confusion and diffusion

Shannon's two design principles:

  • Confusion: make the relationship between key and ciphertext complex (non-linear S-boxes substitute bytes).
  • Diffusion: spread each plaintext bit's influence over many ciphertext bits (permutations and linear mixing).

Ciphers apply many rounds of substitution and permutation with round keys derived from the main key — a substitution–permutation network (AES) or a Feistel network (DES).

DES and its downfall

DES (1977) used a 56-bit key and 64-bit blocks. Its design held up well, but 56 bits is too few: in 1998 the EFF's "Deep Crack" machine brute-forced a key in 56 hours; today it takes hours on rented hardware. 3DES (DES three times, 112-bit effective strength) patched it but is slow, and its 64-bit block causes problems after ~32 GB under one key (Sweet32 birthday attack). Both are retired.

AES

AES (Rijndael, chosen by open competition in 2001) is the global standard block cipher.

PropertyValue
Block size128 bits (a 4×4 grid of bytes — the "state")
Key sizes128, 192 or 256 bits
Rounds10, 12 or 14

Each round applies four steps to the state:

  1. SubBytes: replace each byte using a fixed S-box (non-linear — confusion).
  2. ShiftRows: rotate row i left by i bytes (spreads bytes across columns).
  3. MixColumns: mix each column with a matrix multiplication over GF(2⁸) (diffusion).
  4. AddRoundKey: XOR with the round key.

After a couple of rounds, every output bit depends on every input bit. The best known attacks on full AES are only marginally better than brute force; AES-128 is secure for the foreseeable future (AES-256 is preferred for long-term and post-quantum margins, since Grover's algorithm would halve effective key strength).

Hardware acceleration: AES-NI instructions on x86 (and ARMv8 crypto extensions) run AES rounds in hardware — several GB/s per core — and avoid the cache-timing side channels of table-based software implementations.

AES or ChaCha20?

AES-GCMChaCha20-Poly1305
With hardware supportFastestFast
Without hardware supportSlower; risk of timing leaksFast and constant-time
Nonce96 bits96 bits (XChaCha20: 192 bits — safe to choose randomly)
Used inTLS, disk encryption, cloud KMSTLS, WireGuard, SSH, mobile

Both are excellent. Use whichever your library provides as an AEAD (Authenticated encryption) — never raw AES or raw ChaCha20 without integrity protection.

Python
from cryptography.hazmat.primitives.ciphers.aead import AESGCM, ChaCha20Poly1305
import os

key = AESGCM.generate_key(bit_length=256)
aead = AESGCM(key)
nonce = os.urandom(12)                                # unique per message under this key
ct = aead.encrypt(nonce, b"transfer 500 to Asha", b"order-42")   # associated data: authenticated, not encrypted
pt = aead.decrypt(nonce, ct, b"order-42")             # raises InvalidTag if anything was altered

Key management

The cipher is rarely the weak point — key handling is:

  • Generate keys with a CSPRNG (or derive them with a KDF from a shared secret).
  • Store them outside the data they protect: a cloud KMS, an HSM, or OS keychains; never in source code.
  • Envelope encryption: encrypt data with a per-object data key, and encrypt (wrap) that data key with a master key held in the KMS. Rotating the master key only requires re-wrapping small data keys.
  • Rotate keys periodically and after suspected compromise; limit how much data one key encrypts.
  • Separate keys by purpose (encryption vs MAC vs signing) and environment.
Key takeaways
  • Symmetric ciphers use one shared key and are fast enough for bulk data.
  • The one-time pad is perfectly secret but impractical; reusing a key or nonce leaks plaintext XORs.
  • Stream ciphers (ChaCha20) generate a keystream; block ciphers (AES) permute fixed blocks and need a mode.
  • Confusion (S-boxes) and diffusion (mixing) over many rounds make ciphers strong.
  • DES fell to brute force; AES (128/256-bit keys, hardware-accelerated) is the standard; ChaCha20 excels without hardware support.
  • Always use an AEAD mode, unique nonces, and proper key management (KMS, envelope encryption, rotation).