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.

Intermediate 5 min readLesson 6 of 12

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 problemEasy directionHard directionUsed by
Integer factorisationMultiply two large primesFactor the productRSA
Discrete logarithmCompute gˣ mod pFind x from gˣ mod pDiffie–Hellman, DSA
Elliptic-curve discrete logCompute k·G on a curveFind k from k·GECDH, ECDSA, Ed25519
Lattice problems (LWE)Multiply and add small noiseRecover the secret from noisy equationsML-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.

Encryption
  • Encrypt with the recipient's public key.
  • Decrypt with the recipient's private key.
  • Goal: only the recipient can read it.
Signature
  • 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:

  1. Generate a random symmetric key (e.g. 256-bit AES).
  2. Encrypt the bulk data with it using an AEAD (Authenticated encryption).
  3. Protect the symmetric key with public-key crypto — encrypt it to the recipient's public key, or derive it via key exchange.
  4. 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.

AliceMalloryBobBob's public keyMallory's public key (claims to be Bob's)secret encrypted to Malloryre-encrypted to Bob

Public keys must be authenticated — bound to an identity by something you already trust:

ApproachHowUsed by
Certificate authorities (PKI)A trusted CA signs a certificate binding a public key to a domain nameHTTPS/TLS (PKI and certificates)
Trust on first use (TOFU)Remember the key the first time; warn if it changesSSH (known_hosts)
Out-of-band verificationCompare fingerprints or safety numbers in person or over another channelSignal, WhatsApp
Web of trustPeople sign each other's keysPGP
Transparency logsPublish all keys/certificates in public append-only logs to detect misissuanceCertificate Transparency, key transparency in messaging

Key sizes and security levels

Different algorithms need very different key sizes for the same security:

Security levelSymmetricRSA / DHElliptic curveHash output
112 bits3DES2048224224
128 bits (standard)AES-1283072256 (P-256, X25519)256
192 bitsAES-1927680384384
256 bitsAES-25615360512512

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.
Key takeaways
  • 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.