How public-key cryptography works
RSA, Diffie-Hellman and elliptic curves explained through the hard maths problems they rely on, and where each one shows up in your stack.
On this page
- The trick: one-way functions with a trapdoor
- RSA: the factoring problem
- RSA key sizes and what they buy
- RSA in key exchange
- Diffie-Hellman: the discrete logarithm problem
- Forward secrecy
- Elliptic curves: the same problem, smaller keys
- Where public-key crypto hides in a typical stack
- Signatures versus key exchange: different risks
- What replaces them?
The previous article showed that public-key cryptography does two jobs: agreeing on keys and signing. This one explains how it works, without heavy maths. The goal is to see that every widely deployed public-key algorithm stands on one of two closely related maths problems, because that is exactly the foundation a quantum computer knocks away.
The trick: one-way functions with a trapdoor
Public-key cryptography needs operations that are easy in one direction and hard to reverse, unless you know a secret.
An everyday analogy is mixing paint. Mixing yellow and blue to get green is easy. Looking at a pot of green and working out the exact shades and proportions that made it is hard. Cryptographers want mathematical versions of paint mixing where "hard" means "would take every computer on Earth longer than the age of the universe".
Two families of such problems carry almost all of today's internet security:
| Problem | Easy direction | Hard direction | Used by |
|---|---|---|---|
| Integer factorisation | Multiply two big primes p × q = n | Given n, find p and q | RSA |
| Discrete logarithm | Compute g^x (in a group) | Given g^x, find x | Diffie-Hellman, DSA, ECDH, ECDSA, Ed25519, X25519 |
Both are hard for ordinary computers. As the next article explains, both are easy for a large quantum computer running Shor's algorithm, and for the same underlying reason: both hide a repeating pattern (a "period") that quantum computers are unusually good at finding.
RSA: the factoring problem
RSA (1977) is the oldest widely used public-key system and is still everywhere: TLS certificates, code signing, SSH keys, JWTs signed with RS256, S/MIME email, smart cards and hardware security modules.
How an RSA key is made (simplified):
- Pick two large random primes, p and q (each about 1,024 bits for RSA-2048).
- Multiply them: n = p × q. This is the public modulus.
- Pick a public exponent e (almost always 65537).
- Using p and q, compute a private exponent d that "undoes" e.
public key = (n, e) ← publish this
private key = d (and p, q) ← guard thisTo encrypt a number m: compute m^e mod n. To decrypt: raise the result to d, mod n, and you get m back. Signing is the same in reverse: the private key "encrypts" a hash, and anyone can check it with the public key.
Computing d requires knowing p and q. Anyone who can factor n recovers p and q, computes d, and owns the key. The best classical factoring algorithm, the general number field sieve, is "sub-exponential": faster than trying every possibility, but still far too slow for 2048-bit numbers. The largest RSA challenge number publicly factored is RSA-250 (829 bits), in 2020, which took thousands of CPU-core-years.
RSA key sizes and what they buy
| RSA modulus | Approx. classical security | Status (NIST SP 800-57 / SP 800-131A) |
|---|---|---|
| 1024-bit | ~80 bits | Disallowed; treat as broken |
| 2048-bit | ~112 bits | Today's minimum |
| 3072-bit | ~128 bits | Recommended for longer-lived keys |
| 4096-bit | ~140+ bits | Common in PKI roots |
A crucial point: bigger RSA keys do not save you from quantum computers. Shor's algorithm scales polynomially with key size, so going from 2048 to 4096 bits makes a quantum attack moderately more expensive, not impossible. Bigger keys are a classical fix only.
RSA in key exchange
Older TLS versions (1.2 and earlier) could use RSA key transport: the browser picks a secret, encrypts it with the server's RSA public key, and sends it over. That has a nasty property: if the server's RSA private key ever leaks, every past recorded session can be decrypted. TLS 1.3 removed RSA key transport for this reason; it always uses ephemeral Diffie-Hellman-style key exchange, which gives forward secrecy (more on that below). RSA in TLS 1.3 only signs.
Diffie-Hellman: the discrete logarithm problem
Diffie-Hellman (1976) lets two people agree on a shared secret in public. The classic analogy uses paint:
Alice public Bob
secret colour: red common colour: yellow secret colour: blue
yellow + red = orange ──── orange ────►
◄──── green ──── yellow + blue = green
green + red = BROWN orange + blue = BROWNBoth end up with the same brown. An eavesdropper sees yellow, orange and green, but un-mixing paint to recover red or blue is hard.
In the real version, "colours" are numbers, "mixing" is exponentiation in a mathematical group, and "un-mixing" is the discrete logarithm problem: given g and g^a, find a. Classic "finite-field" Diffie-Hellman (often called DH or FFDH, with groups like ffdhe2048) works with numbers modulo a big prime, and needs 2048-bit or larger numbers for good security.
Forward secrecy
When each connection uses a fresh, throwaway Diffie-Hellman key pair (called ephemeral, the "E" in DHE and ECDHE), the shared secret disappears when the connection ends. Stealing the server's long-term certificate key later doesn't help decrypt old recordings. This property, forward secrecy, is a big reason TLS 1.3 is safer than its predecessors.
But forward secrecy only protects against key theft. It doesn't help if the attacker can solve the discrete logarithm from the recorded public values, which is precisely what a quantum computer can do. That is the core of the harvest-now-decrypt-later problem.
Elliptic curves: the same problem, smaller keys
In the 1980s, Neal Koblitz and Victor Miller independently proposed doing Diffie-Hellman-style maths on elliptic curves: sets of points satisfying an equation like y² = x³ + ax + b over a finite field. You can "add" points on a curve, and adding a point to itself k times (scalar multiplication) is easy, while recovering k from the result is the elliptic curve discrete logarithm problem (ECDLP).
The best known classical attacks on ECDLP are much weaker than those on factoring, so elliptic curves need far smaller keys:
| Security level | RSA / finite-field DH | Elliptic curve |
|---|---|---|
| ~112 bits | 2048-bit | 224-bit |
| ~128 bits | 3072-bit | 256-bit (P-256, Curve25519) |
| ~192 bits | 7680-bit | 384-bit (P-384) |
| ~256 bits | 15360-bit | 521-bit (P-521) |
Smaller keys mean faster handshakes and smaller certificates, so elliptic curves took over most of the internet in the 2010s. The algorithms you'll meet most:
- ECDH / ECDHE: Diffie-Hellman on curves such as NIST P-256 and P-384.
- X25519: Diffie-Hellman on Curve25519 (RFC 7748); the most common TLS 1.3 key exchange before hybrids arrived.
- ECDSA: signatures on NIST curves (ES256 in JWTs, many TLS certificates, Bitcoin and Ethereum on the secp256k1 curve).
- Ed25519 / EdDSA: signatures on the Edwards form of Curve25519 (RFC 8032); popular for SSH keys, Git signing and modern protocols.
Here's the uncomfortable part: because elliptic curve keys are smaller, the quantum computer needed to break a 256-bit curve is estimated to be smaller than the one needed for RSA-2048. Elliptic curves are not a quantum refuge; if anything, they fall first.
Where public-key crypto hides in a typical stack
Public-key algorithms show up in many more places than "the TLS certificate". A quick tour:
Browser ──TLS (X25519 / ECDHE + RSA or ECDSA certificate)──► CDN / load balancer
│
TLS to origin (often older config!)
▼
App server ── JWT (RS256 / ES256 / EdDSA) ── OAuth / OIDC provider
│ ── mTLS to internal services (private CA, RSA or ECDSA)
│ ── database TLS, message queue TLS
│ ── encrypting data keys with RSA-OAEP or ECIES (envelope encryption)
│ ── webhook signatures (often HMAC: symmetric, not affected)
CI/CD ── SSH deploy keys (ed25519 / rsa) ── code & container signing (RSA / ECDSA)
Devices ── firmware signatures, secure boot, smart cards, HSM-held keysThis sprawl is why migrating is a project, not a library bump, and why pqe starts with an inventory: npx -y pqe-cli scan . lists each place in your code that uses RSA, ECDSA, ECDH, Ed25519, X25519 and friends, with file and line numbers.
Signatures versus key exchange: different risks
It's worth repeating the distinction from the first article because it drives every prioritisation decision later:
| Key exchange (RSA-KEM, DH, ECDH, X25519) | Signatures (RSA, ECDSA, Ed25519) | |
|---|---|---|
| Protects | Confidentiality of the session | Authenticity and integrity |
| Can a recording be attacked later? | Yes: break the exchange, decrypt the recording | Not usefully: forging needs to happen live |
| Main quantum risk | Harvest now, decrypt later | Forgery once a quantum computer exists |
| Long-lived exceptions | — | Root CA keys, firmware and secure-boot keys, legal documents meant to stay verifiable for decades |
What replaces them?
The replacements, which the rest of this series covers, are built on different hard problems that no known quantum algorithm solves efficiently:
- Lattices (ML-KEM for key exchange, ML-DSA and FN-DSA for signatures): finding short vectors in high-dimensional grids, made noisy on purpose.
- Hash functions (SLH-DSA, and the stateful schemes LMS and XMSS): security reduces to the hash function itself.
- Error-correcting codes (HQC): decoding a random-looking code.
They do the same jobs, so the shape of your protocols barely changes. What changes is the size of keys and signatures (often kilobytes rather than tens of bytes) and the APIs you call. The new standards covers them in detail.