Public-Key Cryptography
Asymmetric cryptography — the key distribution problem, key pairs, trapdoor one-way functions, encryption vs signatures vs key exchange, hybrid encryption and KEMs, the man-in-the-middle problem and why we need certificates, key sizes, and post-quantum cryptography.
Symmetric encryption has a chicken-and-egg problem: to communicate securely, both parties need a shared key — but how do you share it securely in the first place? With n people, every pair needs its own key: n(n−1)/2 keys. On the internet, you've never met the server you're about to talk to.
Public-key (asymmetric) cryptography, invented in the 1970s (Diffie–Hellman 1976, RSA 1977; secretly earlier at GCHQ), solved this. It's arguably the most important idea in applied cryptography.
Key pairs
Each party generates a key pair:
- A public key — published to the world.
- A private key — never leaves the owner.
They're mathematically linked, but deriving the private key from the public key is computationally infeasible. Security rests on a trapdoor one-way function: easy to compute in one direction, infeasible to invert — unless you know a secret (the trapdoor).
| Hard problem | Easy direction | Hard direction | Used by |
|---|---|---|---|
| Integer factorisation | Multiply two large primes | Factor the product | RSA |
| Discrete logarithm | Compute gˣ mod p | Find x from gˣ mod p | Diffie–Hellman, DSA |
| Elliptic-curve discrete log | Compute k·G on a curve | Find k from k·G | ECDH, ECDSA, Ed25519 |
| Lattice problems (LWE) | Multiply and add small noise | Recover the secret from noisy equations | ML-KEM, ML-DSA (post-quantum) |
Three things public keys do
1. Encryption
Anyone can encrypt with Bob's public key; only Bob's private key decrypts. Like a padlock anyone can snap shut, but only the keyholder can open.
2. Digital signatures
Alice signs with her private key; anyone verifies with her public key. Proves the message came from Alice and wasn't changed — and Alice can't later deny it (Digital signatures).
3. Key exchange
Two parties combine their own private key with the other's public key to derive the same shared secret, without ever transmitting it (Diffie–Hellman). This is how TLS establishes session keys.
- Encrypt with the recipient's public key.
- Decrypt with the recipient's private key.
- Goal: only the recipient can read it.
- Sign with the sender's private key.
- Verify with the sender's public key.
- Goal: everyone can check who wrote it.
Hybrid encryption
Public-key operations are slow (RSA decryption is ~1,000× slower than AES) and can only process small inputs. So real systems use hybrid encryption:
- Generate a random symmetric key (e.g. 256-bit AES).
- Encrypt the bulk data with it using an AEAD (Authenticated encryption).
- Protect the symmetric key with public-key crypto — encrypt it to the recipient's public key, or derive it via key exchange.
- Send both. The recipient recovers the symmetric key with their private key, then decrypts the data.
Modern designs express step 3 as a KEM (key encapsulation mechanism): encapsulate(public_key) → (shared_secret, encapsulation) and decapsulate(private_key, encapsulation) → shared_secret. HPKE (Hybrid Public Key Encryption, RFC 9180) standardises the whole construction and is used by TLS Encrypted Client Hello and MLS. PGP, S/MIME, age and TLS all follow the hybrid pattern.
The man-in-the-middle problem
Public-key crypto lets you encrypt to Bob's public key — but how do you know it's really Bob's? If Mallory intercepts the exchange and substitutes her own public key, you'll encrypt to her; she decrypts, reads, re-encrypts to Bob, and neither side notices.
Public keys must be authenticated — bound to an identity by something you already trust:
| Approach | How | Used by |
|---|---|---|
| Certificate authorities (PKI) | A trusted CA signs a certificate binding a public key to a domain name | HTTPS/TLS (PKI and certificates) |
| Trust on first use (TOFU) | Remember the key the first time; warn if it changes | SSH (known_hosts) |
| Out-of-band verification | Compare fingerprints or safety numbers in person or over another channel | Signal, WhatsApp |
| Web of trust | People sign each other's keys | PGP |
| Transparency logs | Publish all keys/certificates in public append-only logs to detect misissuance | Certificate Transparency, key transparency in messaging |
Key sizes and security levels
Different algorithms need very different key sizes for the same security:
| Security level | Symmetric | RSA / DH | Elliptic curve | Hash output |
|---|---|---|---|---|
| 112 bits | 3DES | 2048 | 224 | 224 |
| 128 bits (standard) | AES-128 | 3072 | 256 (P-256, X25519) | 256 |
| 192 bits | AES-192 | 7680 | 384 | 384 |
| 256 bits | AES-256 | 15360 | 512 | 512 |
Elliptic curves give far smaller keys and faster operations than RSA at equal strength, which is why new systems prefer them.
Post-quantum cryptography
A large quantum computer running Shor's algorithm would factor integers and solve discrete logarithms efficiently, breaking RSA, DH and elliptic curves entirely. (Symmetric crypto and hashes are only weakened by Grover's algorithm — doubling key sizes suffices.)
Adversaries can harvest now, decrypt later: record encrypted traffic today and decrypt it once quantum computers exist. So migration has started:
- NIST standardised ML-KEM (from Kyber, lattice-based key encapsulation), ML-DSA (Dilithium) and SLH-DSA (SPHINCS+, hash-based) signatures in 2024.
- Browsers, Cloudflare, Signal and iMessage deploy hybrid key exchange (e.g. X25519 + ML-KEM-768): secure if either component holds.
- Trade-offs: much larger keys and signatures (kilobytes instead of tens of bytes).
- Organisations are inventorying their cryptography for crypto-agility — the ability to swap algorithms.
- Public-key crypto solves key distribution: publish a public key, keep the private key secret.
- Security relies on trapdoor problems: factoring, discrete logs, elliptic-curve discrete logs, lattices.
- Three uses: encryption (to a public key), signatures (with a private key), key exchange (shared secret).
- Real systems use hybrid encryption: public-key crypto for a fresh symmetric key, AEAD for data (KEMs, HPKE).
- Public keys must be authenticated against man-in-the-middle attacks: PKI, TOFU, fingerprints, transparency logs.
- ECC needs far smaller keys than RSA; quantum computers would break both — hence ML-KEM/ML-DSA and hybrid deployments.