courses

Cryptography

The mathematics under every other control

Cryptography provides the mathematical foundations for confidentiality, integrity, and authentication — the goals named in the CIA triad and pursued, in one form or another, by nearly every page in this course. It is the machinery underneath the controls you meet elsewhere: the signatures behind SPF/DKIM/DMARC and DANE, the chain of trust in DNSSEC, the key exchange and forward secrecy inside VPNs and IPSec, the password hashing and MFA in identity and access management, and the code signing that lets a host verify what it runs.

This page covers the primitives in the order you’d build them up: symmetric and asymmetric encryption, key exchange and forward secrecy, hashing and MACs, digital signatures, TLS, PKI, and key management — each with runnable Python and command-line examples, and, for the public-key schemes, a small-number worked example you can check on paper.

⚠️ Don’t roll your own crypto. Everything here is about correctly using well-vetted primitives, never reimplementing them. The single most common cryptographic vulnerability is a developer building their own scheme. Use the library, use the standard mode, use the recommended parameters.

Symmetric Encryption

Symmetric encryption uses the same key for both encryption and decryption. It is fast and suitable for bulk data encryption.

AES

AES (Advanced Encryption Standard) is the standard symmetric cipher. It operates on 128-bit blocks with key sizes of 128, 192, or 256 bits.

The idea in one paragraph. AES is a substitution–permutation network. It loads 16 plaintext bytes into a 4×4 byte grid called the state, then stirs that grid through 10, 12, or 14 nearly identical rounds (for 128-, 192-, and 256-bit keys respectively). Each round applies four reversible steps:

Step What it does Why it’s there
SubBytes Replace every byte using one fixed 256-entry lookup table, the S-box Confusion — makes the key/ciphertext relationship nonlinear
ShiftRows Rotate row 1 left by one byte, row 2 by two, row 3 by three Diffusion — moves bytes between columns
MixColumns Treat each column as a polynomial and multiply it by a fixed one Diffusion — every output byte depends on all four inputs of its column
AddRoundKey XOR the state with a 16-byte round key derived from the master key Injects the key material

Two details of the schedule matter: an AddRoundKey runs before round 1 (so the plaintext is never exposed as a bare state), and the final round omits MixColumns — without that omission the last mixing step would be trivially undoable and buy nothing. Only AddRoundKey touches the key at all; the other three steps are fixed, public shuffles that an attacker can run as easily as you can. Decryption applies each step’s inverse in reverse order. The “hard math” underneath is small enough to compute by hand — MixColumns multiplies in the finite field GF(2⁸), whose primitive operation is doubling:

# Multiply a byte by 2 in GF(2^8). If the high bit was set, the result
# overflowed 8 bits, so reduce it by AES's polynomial 0x11b (FIPS-197 §4.2).
def xtime(b: int) -> int:
    return ((b << 1) ^ 0x1b) & 0xff if b & 0x80 else b << 1

print(hex(xtime(0x57)))     # -> 0xae    plain doubling, no overflow
print(hex(xtime(0xae)))     # -> 0x47    high bit was set, so reduced

That, a table lookup (S-box(0x53) = 0xED), a row rotation, and an XOR are the whole cipher. The security comes from repeating simple, well-chosen operations enough times that one changed input bit scrambles the entire output — a property you can measure, below.

The mode of operation matters at least as much as the cipher

Mode IV required Authenticated Use case
ECB No No Never use — identical plaintext blocks produce identical ciphertext
CBC Yes No Legacy; requires separate HMAC for integrity
GCM Yes (nonce) Yes (AEAD) Recommended — provides confidentiality and integrity together
CTR Yes (nonce) No Streaming; combine with HMAC
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os

# AES-256-GCM: authenticated encryption (recommended)
key = os.urandom(32)          # 256-bit key — generate securely, store in Vault
nonce = os.urandom(12)        # 96-bit nonce — must be unique per encryption

aesgcm = AESGCM(key)

# Encrypt
plaintext = b"sensitive configuration value"
aad = b"context-authenticated-not-encrypted"   # additional authenticated data
ciphertext = aesgcm.encrypt(nonce, plaintext, aad)

# Decrypt — raises InvalidTag if ciphertext or aad was tampered with
recovered = aesgcm.decrypt(nonce, ciphertext, aad)
assert recovered == plaintext
# Fernet: high-level symmetric encryption (AES-128-CBC + HMAC-SHA256)
# Use when you want a simpler API and don't need raw AES-GCM control
from cryptography.fernet import Fernet

key = Fernet.generate_key()          # 32-byte URL-safe base64 key
f = Fernet(key)

token = f.encrypt(b"my secret data")
plaintext = f.decrypt(token)

Worked example: measuring diffusion, and watching ECB leak

AES on its own is a deterministic function: the same key and the same 16-byte block always produce the same 16 bytes out. That is precisely why the mode wrapped around it matters.

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

# The FIPS-197 Appendix C.1 test vector — a published, known-good AES-128 answer
key = bytes.fromhex('2b7e151628aed2a6abf7158809cf4f3c')
pt  = bytes.fromhex('3243f6a8885a308d313198a2e0370734')

def aes_ecb(data: bytes) -> bytes:        # raw ECB — for demonstration only
    enc = Cipher(algorithms.AES(key), modes.ECB()).encryptor()
    return enc.update(data) + enc.finalize()

print(aes_ecb(pt).hex())
# -> 3925841d02dc09fbdc118597196a0b32        matches FIPS-197

# Avalanche: flip exactly ONE bit of the plaintext and re-encrypt
flipped = bytearray(pt); flipped[15] ^= 0x01
ct1, ct2 = aes_ecb(pt), aes_ecb(bytes(flipped))
print(ct2.hex())
# -> 30a25d6a5c95dde2390758b150ff7038        nothing in common with ct1
diff = int.from_bytes(ct1, 'big') ^ int.from_bytes(ct2, 'big')
print(bin(diff).count('1'), "of 128 bits changed")     # -> 64 of 128 bits changed

# The ECB failure: encrypt the SAME block twice in one message
print(aes_ecb(pt * 2).hex())
# -> 3925841d02dc09fbdc118597196a0b32 3925841d02dc09fbdc118597196a0b32
#    ^^^^^^^^ identical halves: the ciphertext announces that the plaintext repeated

Half the output bits flipped from a single input bit — the avalanche effect, and the payoff of “simple operations, repeated enough times.” But the last line is the catch. Because the cipher is a perfect deterministic lookup, ECB turns repeated plaintext blocks into repeated ciphertext blocks, which is enough to recognize the outline of a bitmap or the structure of a record. CBC, CTR, and GCM all fix this by making each block’s encryption depend on a unique IV or nonce, so identical plaintext never encrypts identically twice.

Key derivation

When you need to derive a key from a password (rather than generating a random key), use a key derivation function (KDF). Never use raw SHA-256 or MD5 to “hash” a password into a key.

from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
import base64
import os

password = b"user-entered-passphrase"
salt = os.urandom(16)          # store this alongside the derived key/ciphertext

kdf = PBKDF2HMAC(
    algorithm=hashes.SHA256(),
    length=32,                 # 256-bit key
    salt=salt,
    iterations=600_000,        # NIST SP 800-132 recommends 600,000 for SHA-256
)
key = kdf.derive(password)

Asymmetric Encryption

Asymmetric (public-key) cryptography uses a mathematically linked key pair: a public key (shareable) and a private key (secret). Data encrypted with the public key can only be decrypted with the private key.

RSA

RSA is the classical asymmetric algorithm. For new deployments, prefer RSA-4096 or elliptic curve algorithms.

RSA rests on a single asymmetry: multiplying two large primes together is easy; factoring the product back into those primes is not. Everybody may know the product; only the key holder knows the factors, and knowing the factors is what lets you undo the encryption. The entire algorithm is five steps of modular arithmetic:

  1. Pick two primes p and q. Their product n = p·q is the modulus, and it is public.
  2. Compute φ(n) = (p−1)(q−1) — how many numbers below n share no factor with it. You can only compute this cheaply if you know p and q.
  3. Pick a public exponent e with gcd(e, φ(n)) = 1. In practice everyone uses e = 65537.
  4. Compute the private exponent d, the modular inverse of e: e·d ≡ 1 (mod φ(n)).
  5. Encrypt with c = mᵉ mod n; decrypt with m = c^d mod n.

Steps 4 and 5 are where the trick lives: raising to e and then to d lands you back where you started, because e·d is one more than a multiple of φ(n) and Euler’s theorem says m^φ(n) ≡ 1 (mod n). The public key is the pair (n, e); the private key is (n, d). An attacker holding (n, e) who could factor n would recover φ(n), then d, then every message — which is why an RSA key size is really a statement about how hard it is to factor a number that long.

Worked example: RSA with small numbers

Real RSA uses 2048- or 4096-bit numbers. The arithmetic is identical with two-digit primes, so here is the whole algorithm at a size you can check on paper.

Steps 1–2 — choose primes, compute the modulus.

p = 11,  q = 13

n    = p·q         = 11 × 13 = 143      ← public modulus
φ(n) = (p−1)(q−1)  = 10 × 12 = 120      ← secret; needs p and q to compute

Step 3 — choose a public exponent coprime to 120. e = 7 works, since gcd(7, 120) = 1. (e = 3 and e = 5 would not: 3 and 5 both divide 120.)

Step 4 — find the private exponent d with 7·d ≡ 1 (mod 120):

7 × 103 = 721 = (6 × 120) + 1   →   721 mod 120 = 1   ✓

d = 103

So the public key is (n = 143, e = 7) and the private key is (n = 143, d = 103).

Step 5 — encrypt. Messages are integers smaller than n; take m = 9:

c = m^e mod n = 9^7 mod 143

You never compute 9⁷ = 4,782,969 and then reduce. You reduce as you go, by square-and-multiply — because 7 = 4 + 2 + 1:

9^1 mod 143 = 9
9^2 mod 143 = 81
9^4 mod 143 = 81² mod 143 = 6561 mod 143 = 126      (143 × 45 = 6435)

9^7 = 9^4 · 9^2 · 9^1:
      126 × 81 mod 143 = 10206 mod 143 = 53         (143 × 71 = 10153)
       53 ×  9 mod 143 =   477 mod 143 = 48         (143 ×  3 =   429)

c = 48

Decrypt by raising the ciphertext to d:

m = c^d mod n = 48^103 mod 143 = 9      ✓ the original message

Python does the whole thing, modular inverse included, in a handful of lines:

from math import gcd

p, q = 11, 13
n   = p * q                   # 143  — public modulus
phi = (p - 1) * (q - 1)       # 120  — secret
e   = 7
assert gcd(e, phi) == 1
d   = pow(e, -1, phi)         # 103  — modular inverse; Python 3.8+

m = 9
c = pow(m, e, n)              # 48   — encrypt with the PUBLIC exponent
print(c, pow(c, d, n))        # -> 48 9    decrypt with the PRIVATE exponent

# A signature is the same operation with the two keys swapped:
sig = pow(m, d, n)            # 113  — sign with the PRIVATE exponent
print(sig, pow(sig, e, n))    # -> 113 9   verify with the PUBLIC exponent

The three-argument pow(e, -1, phi) is Python’s modular inverse, accepted since 3.8; before that you wrote out the extended Euclidean algorithm yourself.

Those five numbers are not a toy construct — they are exactly what a real key file holds, just much larger:

openssl genrsa -out demo.pem 2048
openssl rsa -in demo.pem -text -noout | head -1
# -> Private-Key: (2048 bit, 2 primes)

The rest of that dump labels the same quantities: modulus is n, publicExponent is e (65537), privateExponent is d, and prime1/prime2 are p and q — the secret factors, which real tooling keeps in the private key file so it can decrypt faster via the Chinese Remainder Theorem.

⚠️ The example above is also three separate demonstrations of why toy RSA is useless. Each is an attack that real RSA has to defend against:

  • The modulus factors instantly. 143 = 11 × 13. Anyone who sees the public key recovers φ(n) = 120, then d = 103, then every message ever sent. Security is the cost of factoring n; 2048 bits is today’s floor, and 4096-bit RSA or elliptic curve is the recommendation for new keys.
  • It is deterministic. m = 9 always encrypts to 48. An attacker who suspects the plaintext is one of a few values — “yes”/”no”, a Social Security number, a card number — simply encrypts each candidate with the public key and compares ciphertexts. No key recovery needed.
  • Small messages never wrap. With e = 7 and m = 2, 2⁷ = 128 is less than n = 143, so the modular reduction never happens and the “ciphertext” is undone by taking an ordinary 7th root.

OAEP padding fixes the last two by mixing the message with fresh randomness and expanding it to the full width of the modulus before exponentiation, so the same plaintext encrypts differently every time and always wraps. This is why the code below uses padding.OAEP and why raw “textbook” RSA appears nowhere outside a classroom. The construction is specified in RFC 8017.

RSA in practice

Having seen the arithmetic, do not implement it. Use the library:

from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes, serialization

# Generate RSA key pair
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=4096,
)
public_key = private_key.public_key()

# Serialize private key to PEM (for storage)
pem_private = private_key.private_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PrivateFormat.PKCS8,
    encryption_algorithm=serialization.BestAvailableEncryption(b"key-passphrase"),
)

# Encrypt with public key (OAEP padding — required; PKCS1v15 is deprecated)
ciphertext = public_key.encrypt(
    b"small secret (RSA max payload is limited by key size)",
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None,
    ),
)

# Decrypt with private key
plaintext = private_key.decrypt(ciphertext, padding.OAEP(
    mgf=padding.MGF1(algorithm=hashes.SHA256()),
    algorithm=hashes.SHA256(),
    label=None,
))

Elliptic Curve Cryptography

ECC provides equivalent security to RSA with much smaller key sizes. A 256-bit ECC key is roughly equivalent in security to a 3072-bit RSA key.

Where RSA’s hard problem is factoring, ECC’s is the elliptic-curve discrete logarithm: given a fixed starting point G on a curve and the result of adding G to itself k times, recover k. “Adding” two points has a geometric definition — draw the line through them, find the third place that line crosses the curve, and reflect it across the x-axis. To double a point you use the tangent instead of a chord. Over a finite field the picture disappears, but the formulas survive unchanged, and running them backwards is hard enough that a 256-bit key matches 3072-bit RSA. A private key is simply the scalar k; the public key is the point kG.

Worked example: elliptic curve arithmetic over a tiny field

Take the curve y² = x³ + 2x + 2 over the integers mod 17. It has exactly 19 points — 18 (x, y) solutions plus the point at infinity, which acts as zero — and G = (5, 1) generates all of them.

Doubling G (tangent case, λ is the slope):

λ  = (3x₁² + a) / (2y₁) = (3·25 + 2) / (2·1) = 77 / 2 mod 17
                        = 9 · 2⁻¹ mod 17 = 9 · 9 = 81 mod 17 = 13
x₃ = λ² − 2x₁           = 169 − 10 = 159 mod 17 = 6
y₃ = λ(x₁ − x₃) − y₁    = 13·(5 − 6) − 1 = −14 mod 17 = 3

2G = (6, 3)

Note 2⁻¹ = 9, because 2 × 9 = 18 ≡ 1 (mod 17): “division” in a finite field is multiplication by a modular inverse. Adding two different points uses the chord, λ = (y₂−y₁)/(x₂−x₁), so 3G = G + 2G:

λ  = (3 − 1) / (6 − 5) = 2
x₃ = 2² − 5 − 6 = −7 mod 17 = 10
y₃ = 2·(5 − 10) − 1 = −11 mod 17 = 6

3G = (10, 6)

Both formulas, and the whole group, in a dozen lines:

p, a = 17, 2                       # curve y^2 = x^3 + 2x + 2 over F_17

def add(P, Q):                     # None represents the point at infinity
    if P is None or Q is None:
        return Q if P is None else P
    if P[0] == Q[0] and (P[1] + Q[1]) % p == 0:
        return None                                            # P + (−P) = O
    if P == Q:
        lam = (3 * P[0]**2 + a) * pow(2 * P[1], -1, p) % p      # tangent
    else:
        lam = (Q[1] - P[1]) * pow(Q[0] - P[0], -1, p) % p       # chord
    x = (lam * lam - P[0] - Q[0]) % p
    return (x, (lam * (P[0] - x) - P[1]) % p)

G, point = (5, 1), (5, 1)
for k in range(2, 20):
    point = add(point, G)
    print(f"{k}G = {point}")
# 2G = (6, 3)    3G = (10, 6)   4G = (3, 1)    5G = (9, 16)   6G = (16, 13)
# ...
# 18G = (5, 16)  19G = None     <- back to the identity: the group has order 19

The multiples jump around with no pattern you can exploit: (5,1), (6,3), (10,6), (3,1), (9,16), (16,13)… Computing kG is fast; recovering k from kG is not. Here you could find k by trying all 19 possibilities. On Curve25519 there are roughly 2²⁵² of them, which is why an Ed25519 public key is 32 bytes while a comparable RSA-3072 modulus is 384.

from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey

# Ed25519: fast, secure, recommended for signatures
private_key = Ed25519PrivateKey.generate()
public_key = private_key.public_key()

# Sign a message
message = b"artifact contents to sign"
signature = private_key.sign(message)

# Verify (raises InvalidSignature on failure)
public_key.verify(signature, message)
# OpenSSL: generate and inspect keys from the command line
# Generate an Ed25519 key pair
openssl genpkey -algorithm ed25519 -out private.pem
openssl pkey -in private.pem -pubout -out public.pem

# Generate an ECDSA key (P-256 curve)
openssl ecparam -name prime256v1 -genkey -noout -out ec-private.pem
openssl ec -in ec-private.pem -pubout -out ec-public.pem

# Generate RSA-4096
openssl genrsa -out rsa-private.pem 4096
openssl rsa -in rsa-private.pem -pubout -out rsa-public.pem

Digital Signatures

Digital signatures use the private key to sign, and the public key to verify. They provide authentication (who signed it) and integrity (has it changed).

That is public-key cryptography run backwards. Encryption uses the public key, so anyone may send; signing uses the private key, so only one party can produce the result — and anybody holding the public key can check it. In the toy RSA numbers above, with d = 103 secret and e = 7 public:

sign:    s = m^d mod n =   9^103 mod 143 = 113     (only the key holder can do this)
verify:      s^e mod n = 113^7   mod 143 = 9   ✓   (anyone can do this)

One detail that the toy version hides: you never sign the message, you sign its hash. RSA can only operate on numbers smaller than the modulus, and a hash gives you a fixed-size stand-in for a message of any length. It also means a signature is only as trustworthy as the hash’s collision resistance — find two documents with the same digest and one signature covers both, which is exactly how SHA-1 signatures died. Modern schemes also need a padding scheme (RSA-PSS, not raw exponentiation) for the same reasons OAEP is required for encryption.

from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.asymmetric.utils import (
    decode_dss_signature, encode_dss_signature
)
from cryptography.hazmat.primitives import hashes
from cryptography.exceptions import InvalidSignature

# ECDSA signing
private_key = ec.generate_private_key(ec.SECP256R1())
public_key = private_key.public_key()

artifact = b"binary content to sign"
signature = private_key.sign(artifact, ec.ECDSA(hashes.SHA256()))

# Verification
try:
    public_key.verify(signature, artifact, ec.ECDSA(hashes.SHA256()))
    print("Signature valid")
except InvalidSignature:
    print("Signature INVALID — artifact may have been tampered with")

Key Exchange and Forward Secrecy

Symmetric encryption is fast, but it has a chicken-and-egg problem: both parties need the same key, and the network between them is exactly the place an eavesdropper is listening. How do two parties agree on a shared secret over a wire someone is watching? This is the key-exchange problem, and its solution is one of the most important ideas in all of computing.

Diffie–Hellman

Diffie–Hellman (DH, 1976) lets two parties derive a shared secret without ever transmitting it. Each side mixes its own secret with a public value; the math (modular exponentiation, or point multiplication on an elliptic curve) is arranged so both arrive at the same result, while an eavesdropper who sees only the public values cannot feasibly compute it. The elliptic-curve variant ECDH — especially over Curve25519 (X25519) — is the modern default.

from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes

# Each party generates an ephemeral key pair
alice_priv = X25519PrivateKey.generate()
bob_priv   = X25519PrivateKey.generate()

# They exchange ONLY public keys over the (possibly hostile) network
alice_shared = alice_priv.exchange(bob_priv.public_key())
bob_shared   = bob_priv.exchange(alice_priv.public_key())
assert alice_shared == bob_shared        # same secret, never transmitted

# Never use the raw DH output as a key — run it through a KDF
session_key = HKDF(algorithm=hashes.SHA256(), length=32,
                   salt=None, info=b"handshake").derive(alice_shared)

Worked example: Diffie–Hellman with small numbers

The usual analogy is paint. Alice and Bob agree publicly on a common color; each secretly adds a color of their own and sends the mixture across the room; each then adds their own secret color to what they received. Both end up with the same three-color blend, while an eavesdropper who saw only the two mixtures cannot un-mix them. Modular exponentiation is the arithmetic version of “easy to mix, hard to separate.”

Public parameters, known to everyone including the attacker: a prime p = 23 and a generator g = 5.

Step Alice Bob
Picks a secret, never transmitted a = 6 b = 15
Computes and sends over the wire A = 5⁶ mod 23 = 8 B = 5¹⁵ mod 23 = 19
Receives 19 8
Computes the shared secret 19⁶ mod 23 = 2 8¹⁵ mod 23 = 2

Both land on 2, because both computed 5^(6×15) mod 23 by different routes: (gᵇ)ᵃ = (gᵃ)ᵇ. The network saw p, g, 8, and 19. It never saw 6, 15, or 2.

p, g = 23, 5
a, b = 6, 15                      # private exponents — never transmitted

A = pow(g, a, p)                  # 8    Alice sends this
B = pow(g, b, p)                  # 19   Bob sends this

assert pow(B, a, p) == pow(A, b, p) == 2     # same secret, derived independently

Recovering a from the wire means solving 5ˣ ≡ 8 (mod 23) — the discrete logarithm problem. With p = 23 you try all 22 exponents and you are done. With a 2048-bit prime, or an elliptic curve of comparable strength, no known classical algorithm finishes in any useful amount of time. (Shor’s algorithm on a large quantum computer would, which is the whole reason for the post-quantum transition.)

⚠️ Raw Diffie–Hellman authenticates nobody. It gives you a shared secret with whoever is at the other end — which, in a machine-in-the-middle attack, is the attacker running two separate exchanges. Key exchange must always be combined with authentication: a certificate in TLS, a pre-shared key or certificate in IKEv2, a verified safety number in Signal.

# OpenSSL: derive a shared secret from your private key + their public key
openssl genpkey -algorithm x25519 -out alice.pem
openssl pkey -in alice.pem -pubout -out alice.pub
# ... exchange public keys, then each side runs:
openssl pkeyutl -derive -inkey alice.pem -peerkey bob.pub -out shared.bin

Perfect Forward Secrecy

The crucial move is to make the key-exchange keys ephemeral — generated fresh for each session and thrown away afterward (the E in DHE / ECDHE). This gives Perfect Forward Secrecy (PFS): because the session keys were never derived from the server’s long-term private key, an attacker who later steals that long-term key — or who recorded the encrypted traffic months ago — still cannot decrypt past sessions. Each conversation’s keys died with it.

⚠️ PFS defeats “harvest now, decrypt later.” Adversaries record encrypted traffic today hoping to decrypt it after a future key compromise. Without forward secrecy (e.g. old RSA key-transport cipher suites), one stolen server key unlocks years of captured traffic. With ephemeral key exchange, it unlocks nothing. This is why TLS 1.3 removed static RSA key exchange entirely and mandates (EC)DHE, and why IKEv2 in IPSec negotiates fresh Diffie–Hellman keys per session.

Future secrecy and the Signal protocol

Forward secrecy protects the past after a compromise. The Signal protocol — used by Signal, WhatsApp, and others for end-to-end messaging — adds future secrecy (a.k.a. post-compromise security or “self-healing”): even if an attacker steals a device’s current keys, the conversation recovers security for future messages once a fresh exchange happens. It achieves this with two mechanisms:

This is a sharp contrast with PGP/S-MIME email encryption, which uses long-lived keys and has no forward secrecy — steal the key once and every past message is readable. It is the cryptographic reason secure messengers are preferred over encrypted email for sensitive conversations.

A note on the post-quantum transition

A sufficiently large quantum computer would break the discrete-log and factoring problems that DH, ECDH, and RSA rest on — and “harvest now, decrypt later” means traffic captured today is already at risk. NIST standardized the first replacements in August 2024: ML-KEM (FIPS 203, the key-encapsulation mechanism formerly called CRYSTALS-Kyber) for key exchange, plus ML-DSA (FIPS 204) and SLH-DSA (FIPS 205) for signatures. Deployment is already underway as hybrids that run a classical and a post-quantum exchange together — X25519MLKEM768, on by default in Chrome and Firefox since late 2024 — so the session stays secure as long as either component holds.

Why lattices? The replacements do not rest on factoring or discrete logarithms at all. ML-KEM’s hardness comes from learning with errors (LWE): you are handed a system of linear equations whose right-hand sides are each off by a small random amount, and asked to find the secret. Strip the noise out and it is high-school algebra — Gaussian elimination solves it instantly. Leave the noise in and every near-miss candidate looks about as plausible as the true answer:

secret s = (s₁, s₂) = (5, 7),  arithmetic mod 97,  each equation off by ±1

 1·s₁ + 2·s₂ = 20        true value 19, noise +1
 3·s₁ + 1·s₂ = 21        true value 22, noise −1
 4·s₁ + 6·s₂ = 63        true value 62, noise +1

Three noisy equations in two unknowns mod 97 is still guessable by hand. ML-KEM uses hundreds of unknowns over structured lattices, and no algorithm — classical or quantum — is known to do appreciably better than brute force against it. That is the bet the whole transition rests on.

🔮 This area is moving fast. Treat specific algorithm names, browser defaults, and standard numbers here as a 2024–2025 snapshot and re-verify before relying on them.

Cryptographic Hashing

Hash functions take arbitrary input and produce a fixed-size digest. They are one-way (you cannot reverse them) and collision-resistant (it is computationally infeasible to find two different inputs with the same hash).

Three properties define a cryptographic hash, and each one rules out a specific attack:

Property Means What breaks if it fails
Preimage resistance Given a digest, you cannot find an input that produces it Stored password hashes become reversible
Second-preimage resistance Given one input, you cannot find a different input with the same digest A signed file can be swapped for another
Collision resistance You cannot find any two inputs that collide A CA can be tricked into signing a forged certificate — how MD5 and SHA-1 died

Collision resistance is the weakest of the three and the first to fall, because the birthday paradox halves the work: finding some collision in an n-bit hash costs about 2^(n/2), not 2ⁿ. That is why 128-bit digests are long dead and SHA-256 is the floor.

Worked example: avalanche, and a “hash” that isn’t one

The practical signature of a good hash is that the digest looks completely unrelated to the input — change anything at all, and about half the output bits flip:

import hashlib

for s in (b"hello", b"helln"):      # 'o' = 0x6f, 'n' = 0x6e — exactly one bit apart
    print(s, hashlib.sha256(s).hexdigest())
# b'hello' 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
# b'helln' d1dd3e4f53afb65be5774853d60b74fa12c10b769c262165562c5287e6816e15

a = int(hashlib.sha256(b"hello").hexdigest(), 16)
b = int(hashlib.sha256(b"helln").hexdigest(), 16)
print(bin(a ^ b).count('1'), "of 256 bits differ")     # -> 128 of 256 bits differ

One input bit changed; 128 of 256 output bits changed — exactly what you would expect from a random function, which is the design goal. Compare that with something that merely looks like a hash:

def toy_hash(data: bytes) -> int:
    return sum(data) % 256            # fixed-size output, but NOT a hash

print(toy_hash(b"PAY 100"), toy_hash(b"PAY 010"))      # -> 155 155

It produces a fixed-size digest from arbitrary input, and it is “one-way” in the sense that you cannot read the message out of it. It is still worthless: there is no avalanche (change a byte by one, the output moves by one), and collisions are trivial to construct on purpose — reorder the characters, or add 1 to one byte and subtract 1 from another. Every integrity guarantee on this page depends on collisions being infeasible, not merely inconvenient.

SHA-256 and SHA-3

SHA-256 (from the SHA-2 family) is the current standard for integrity verification. SHA-3 (Keccak) is an alternative with different internal design.

import hashlib

# SHA-256
data = b"file contents or message"
digest = hashlib.sha256(data).hexdigest()
print(digest)   # 64-character hex string

# SHA-512 (stronger; often used for password storage and HMAC)
digest512 = hashlib.sha512(data).hexdigest()

# SHA3-256 (SHA-3 family)
digest3 = hashlib.sha3_256(data).hexdigest()

# Streaming hash for large files
def sha256_file(path: str) -> str:
    h = hashlib.sha256()
    with open(path, 'rb') as f:
        for chunk in iter(lambda: f.read(65536), b''):
            h.update(chunk)
    return h.hexdigest()

print(sha256_file('/etc/os-release'))
# Command-line hash verification
sha256sum myfile.tar.gz                       # compute hash
sha256sum --check SHA256SUMS                  # verify against a manifest

# Generate a hash manifest for a directory
find /etc -type f -exec sha256sum {} \; > /root/etc-checksums.txt
# Later: verify
sha256sum --check /root/etc-checksums.txt

HMAC

HMAC (Hash-based Message Authentication Code) combines a hash function with a secret key, providing both integrity and authentication. Unlike a plain hash, an attacker cannot forge an HMAC without knowing the key.

The obvious way to combine them — H(key ‖ message) — is broken for SHA-256 and the rest of SHA-2, and the reason is worth knowing. Those functions use a Merkle–Damgård construction: they absorb the input block by block into an internal state, and the final state is the published digest. So an attacker who holds a valid tag for some message, without knowing the key at all, can load that digest back in as a starting state and keep hashing — producing a valid tag for message ‖ padding ‖ anything_they_like. This is the length-extension attack, and it is a real-world forgery of signed API requests, not a theoretical curiosity.

HMAC sidesteps it by hashing twice, with two keys derived from yours:

HMAC(K, m) = H( (K ⊕ opad) ‖ H( (K ⊕ ipad) ‖ m ) )

The inner digest is swallowed by an outer hash, so an attacker never sees an internal state they can resume from. (SHA-3 and BLAKE2 are not Merkle–Damgård and are not length-extendable, which is why their keyed modes — KMAC, BLAKE2’s keyed hashing — can be far simpler.)

Worked example: checking HMAC against a published vector

import hmac, hashlib

# RFC 4231, test case 1
key, msg = b'\x0b' * 20, b"Hi There"
print(hmac.new(key, msg, hashlib.sha256).hexdigest())
# -> b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7

Reproducing a published test vector is how you confirm that an implementation — yours, a vendor’s, or a hardware module’s — is computing real HMAC and not something that merely resembles it. The same habit applies to the FIPS-197 AES vector above.

import hmac
import hashlib
import secrets

key = secrets.token_bytes(32)     # 256-bit key — keep secret

# Compute HMAC
message = b"API request body or payload"
mac = hmac.new(key, message, hashlib.sha256).hexdigest()

# Verify (use compare_digest to prevent timing attacks)
def verify_hmac(key: bytes, message: bytes, provided_mac: str) -> bool:
    expected = hmac.new(key, message, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, provided_mac)

Password Hashing

Passwords must never be stored as plain SHA-256 or MD5 hashes — those are too fast and vulnerable to brute-force and rainbow table attacks. Use purpose-built, slow password hashing algorithms.

# bcrypt: the classic recommendation
import bcrypt

password = b"user-password"
hashed = bcrypt.hashpw(password, bcrypt.gensalt(rounds=12))   # store this

# Verify
bcrypt.checkpw(password, hashed)   # returns True/False
# Argon2id: NIST SP 800-63B and OWASP recommended (stronger than bcrypt)
from argon2 import PasswordHasher

ph = PasswordHasher(
    time_cost=3,           # iterations
    memory_cost=65536,     # 64 MB
    parallelism=4,
)

hashed = ph.hash("user-password")   # store this string

try:
    ph.verify(hashed, "user-password")   # returns True or raises exception
    print("Password correct")
    if ph.check_needs_rehash(hashed):    # upgrade parameters over time
        new_hash = ph.hash("user-password")
except Exception:
    print("Password incorrect")

TLS (Transport Layer Security)

TLS is the protocol that secures network communications. It provides authentication (the server presents a certificate), confidentiality (traffic is encrypted), and integrity (MAC prevents tampering). It is the workhorse that ties this whole page together — bulk symmetric encryption keyed by an ephemeral key exchange, the server authenticated by a certificate, and is itself the basis of HTTPS, DoH, and email’s STARTTLS.

TLS handshake overview

Client                                    Server
  │                                          │
  │──── ClientHello (TLS 1.3, ciphers) ────▶│
  │◀─── ServerHello + Certificate ──────────│
  │◀─── CertificateVerify + Finished ───────│
  │──── Finished ──────────────────────────▶│
  │                                          │
  │═══ Encrypted application data ══════════│

TLS 1.3 (RFC 8446) eliminates weak cipher suites and reduces handshake latency to one round-trip. Critically, it mandates ephemeral (EC)DHE key exchange — so every TLS 1.3 session has Perfect Forward Secrecy — and removes the old static-RSA key transport that lacked it. TLS 1.2 is still in widespread use but TLS 1.0 and 1.1 are deprecated.

Generating and inspecting certificates

# Generate a self-signed certificate (for testing only)
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem \
    -days 365 -nodes \
    -subj "/CN=localhost/O=Test/C=US"

# Inspect a certificate
openssl x509 -in cert.pem -text -noout

# Check a remote server's certificate
openssl s_client -connect example.com:443 -showcerts </dev/null 2>/dev/null \
    | openssl x509 -text -noout

# Verify certificate chain
openssl verify -CAfile ca-bundle.crt server.crt

# Check certificate expiry
openssl s_client -connect example.com:443 </dev/null 2>/dev/null \
    | openssl x509 -noout -dates

# Test TLS configuration (cipher suites, protocol versions)
nmap --script ssl-enum-ciphers -p 443 example.com
# Python: enforce TLS in all HTTP requests
import ssl
import requests

# requests uses certifi by default — always verifies certificates
response = requests.get('https://api.example.com/data')   # TLS verified

# Never disable verification in production:
# requests.get(url, verify=False)   # INSECURE — disables certificate validation

# Custom CA bundle (for internal PKI)
response = requests.get('https://internal.example.com', verify='/etc/ssl/internal-ca.crt')

# Client certificate authentication (mTLS)
response = requests.get(
    'https://mtls-api.example.com',
    cert=('/path/to/client.crt', '/path/to/client.key'),
    verify='/path/to/ca.crt',
)

TLS hardening in nginx

# /etc/nginx/conf.d/tls.conf
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    # Protocols: TLS 1.2 minimum; prefer TLS 1.3
    ssl_protocols TLSv1.2 TLSv1.3;

    # Modern cipher suite (Mozilla Intermediate configuration)
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    ssl_prefer_server_ciphers off;

    # HSTS: force HTTPS for 1 year, include subdomains
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;

    # DH parameters (for DHE cipher suites)
    ssl_dhparam /etc/ssl/dhparam.pem;
}

PKI and Certificate Lifecycle

A Public Key Infrastructure (PKI) is the system of certificates, certificate authorities (CAs), and policies that enables trust in public keys. It answers the question key exchange leaves open: how do you know the public key you received actually belongs to the server, and not an attacker performing a machine-in-the-middle? The answer is a chain of digital signatures anchored in CAs your system already trusts.

PKI components

Component Role
Root CA Self-signed certificate at the top of the trust chain; kept offline
Intermediate CA Issues end-entity certificates; online; if compromised, only revoke this CA
End-entity certificate Issued to a server, client, or code-signing identity
Certificate Revocation List (CRL) List of revoked certificates published by the CA
OCSP Online Certificate Status Protocol — real-time revocation check

Certificate lifecycle management

# Generate a certificate signing request (CSR)
openssl req -new -newkey rsa:4096 -keyout server.key -out server.csr -nodes \
    -subj "/CN=api.example.com/O=Example Corp/C=US" \
    -addext "subjectAltName=DNS:api.example.com,DNS:www.api.example.com"

# Sign the CSR with your intermediate CA
openssl x509 -req -in server.csr -CA intermediate.crt -CAkey intermediate.key \
    -CAcreateserial -out server.crt -days 365 \
    -extfile <(echo "subjectAltName=DNS:api.example.com")

# Check expiry across a fleet (run before certificates expire)
for host in api.example.com www.example.com db.example.com; do
    expiry=$(openssl s_client -connect "${host}:443" </dev/null 2>/dev/null \
        | openssl x509 -noout -enddate | cut -d= -f2)
    echo "${host}: expires ${expiry}"
done

cert-manager (Kubernetes)

cert-manager automates certificate issuance and renewal in Kubernetes clusters:

# ClusterIssuer using Let's Encrypt
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-prod
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    email: ops@example.com
    privateKeySecretRef:
      name: letsencrypt-prod-key
    solvers:
      - http01:
          ingress:
            class: nginx
---
# Certificate request
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: api-tls
  namespace: production
spec:
  secretName: api-tls-secret
  issuerRef:
    name: letsencrypt-prod
    kind: ClusterIssuer
  dnsNames:
    - api.example.com

Key Management

Keys are only as secure as the system that stores them. Hardcoding keys in source code, environment variables, or unencrypted files creates serious vulnerabilities — a leaked key is among the most common findings in application security secret-scanning, and it instantly undoes every primitive above.

Key management principles

HashiCorp Vault for key management

See the CI/CD, Secret Management, and GitOps page for a full Vault reference. For cryptographic operations specifically:

# Vault Transit secrets engine: encryption as a service
# Applications encrypt/decrypt through the API — they never see the key

vault secrets enable transit
vault write transit/keys/myapp-key type=aes256-gcm96

# Encrypt data (returns base64 ciphertext)
vault write transit/encrypt/myapp-key \
    plaintext=$(echo -n "sensitive value" | base64)

# Decrypt data
vault write transit/decrypt/myapp-key \
    ciphertext="vault:v1:..."

# Rotate the key (old versions still decrypt old data)
vault write transit/keys/myapp-key/rotate
import hvac
import base64
import os

client = hvac.Client(url=os.environ['VAULT_ADDR'], token=os.environ['VAULT_TOKEN'])

def vault_encrypt(plaintext: str, key_name: str = 'myapp-key') -> str:
    encoded = base64.b64encode(plaintext.encode()).decode()
    result = client.secrets.transit.encrypt_data(name=key_name, plaintext=encoded)
    return result['data']['ciphertext']

def vault_decrypt(ciphertext: str, key_name: str = 'myapp-key') -> str:
    result = client.secrets.transit.decrypt_data(name=key_name, ciphertext=ciphertext)
    return base64.b64decode(result['data']['plaintext']).decode()

AWS KMS

import boto3
import base64

kms = boto3.client('kms', region_name='us-east-1')
KEY_ID = 'arn:aws:kms:us-east-1:123456789012:key/abc-123'

# Encrypt
def kms_encrypt(plaintext: str) -> bytes:
    response = kms.encrypt(KeyId=KEY_ID, Plaintext=plaintext.encode())
    return response['CiphertextBlob']

# Decrypt (KMS looks up the key automatically from the ciphertext metadata)
def kms_decrypt(ciphertext_blob: bytes) -> str:
    response = kms.decrypt(CiphertextBlob=ciphertext_blob)
    return response['Plaintext'].decode()

# Envelope encryption: generate a data key, use it locally
data_key = kms.generate_data_key(KeyId=KEY_ID, KeySpec='AES_256')
plaintext_key = data_key['Plaintext']       # use this to encrypt data, then discard
encrypted_key = data_key['CiphertextBlob']  # store this alongside the encrypted data

Key takeaways

References


Related course pages: Access Control and Authorization · Identity and Access Management · Email Security · DNS Security and Privacy · VPNs and IPSec · Network Security · Application Security

🛠️ Maintenance note: the cryptographic recommendations here drift — re-verify before each term. PBKDF2 iteration counts and Argon2 parameters rise over time; cipher-suite and TLS-version guidance follows the Mozilla generator; and the post-quantum section (FIPS 203/204/205, hybrid X25519MLKEM768, browser defaults) is a 2024–2025 snapshot of a fast-moving transition. Confirm the Python cryptography API calls against the version on the course VM. The small-number worked examples (RSA mod 143, Diffie–Hellman mod 23, the curve over F₁₇, the FIPS-197 and RFC 4231 vectors) are arithmetic and published test data, so they do not go stale — but they are deliberately breakable and must never be presented as deployable parameters.