← Learn Runs in your browser

Already know what the handshake does? /learn/https covers the protocol — certificates, the chain of trust and the TLS message flow. This page goes one level down, into the maths those steps rely on.

Step 1 Two strangers, one open wire

Alice and Bob have never met and share no secret. Every byte between them passes through Eve, who reads and records everything. They still need to agree on a key — and they have to do it in public.

Send something in the clear and watch what Eve gets

Alice
Eve is listening here
Bob

Nothing sent yet.

Encrypting first does not help on its own: whatever key Alice used, she would have to send it down the same wire. The rest of this page is about escaping that circle.

Step 2 One-way functions and clock arithmetic

Work modulo a prime and numbers wrap like a clock face. Raising a number to a power on that clock is quick; asking which power produced a given result is not — there is no shortcut, only search. That gap is the raw material of every public-key scheme.

Forwards: 5^7 mod 23

result

Square-and-multiply walks the exponent bit by bit, so the work grows with the number of digits, not with the number itself — here.

#bitsquaremultiplyrunning value

Backwards: which exponent was it?

not tried yet

Given g, p and the result, the only general method is to try every exponent in turn.

How fast does that search grow?
8 bits

Candidates to try: . Forward cost: . Doubling the key size doubles the forward work and squares the search.

Toy numbers, never use these sizes for real.

Step 3 Diffie-Hellman: agreeing on a secret in public

Alice and Bob each pick a private exponent and publish only the result of raising a shared generator to it. Each then raises the other side's public value to their own private exponent, and both land on the same number — one that never appeared on the wire.

Both sides compute the same secret

Alice

sends A = ga mod p =

computes Ba mod p =

Bob

sends B = gb mod p =

computes Ab mod p =

What Eve sees

Eve never sees a, b or the shared secret — only p, g, A and B.

Toy numbers, never use these sizes for real.

Step 4 RSA: a lock anyone can close and only you can open

Multiply two primes and the product is easy to compute and hard to take apart again. RSA hides the private exponent behind exactly that asymmetry: deriving d needs the factors, and everyone else only gets the product.

Build a key pair

Public — hand this to anyone

n = =

public key

Private — never leaves your machine

phi = =

d = e-1 mod phi =

private key

How d falls out of the extended Euclidean algorithm
#division

Running the divisions backwards expresses 1 as a combination of e and phi, and the coefficient on e is d:

Encrypt and decrypt a number

c = me mod n = cd mod n =

The public exponent locks, the private exponent unlocks, and the round trip returns the original number. Real RSA never encrypts a bare number like this — it pads first (OAEP), because textbook RSA leaks structure.

Why factoring n is the hard part
4 digits

Naive trial division: . Real keys use a 2048-bit n — 617 decimal digits — and even the best known algorithms have never come close to factoring one.

Toy numbers, never use these sizes for real.

Step 5 Signatures are the same maths, run the other way

Encryption goes public key in, private key out. A signature goes the other direction: the private key signs, the public key verifies. You never sign the message itself, only its hash, so the size of the document does not matter.

Sign with d, verify with e

Signing (private key)

signed text:

h = toy-hash(message) mod n =

s = hd mod n =

Verifying (public key)

se mod n =

compare against the hash of the message on screen.

The hash here is the sum of the character codes — a toy, trivially collidable, used only so the numbers stay printable. Real signatures hash with SHA-256 and pad with PSS. The direction is what matters: private signs, public verifies; public encrypts, private decrypts.

Toy numbers, never use these sizes for real.

Step 6 Elliptic curves in one picture

Swap "multiply numbers modulo a prime" for "add points on a curve" and the same one-way structure appears with far smaller keys. Adding two points means drawing the line through them, taking the third crossing, and mirroring it across the x-axis.

y² = x³ + 2x + 3 over F97 — all 99 points

● P and Q ● third crossing (−R) ● R = P + Q ○ the k · G walk

Scalar multiplication: k · G, with G = (0, 10)
k = 8

Adding G to itself k times walks the highlighted dots. Given G and k · G, recovering k is the elliptic-curve discrete logarithm problem — and it resists every shortcut that works on ordinary numbers. That is why a 256-bit curve key matches a 3072-bit RSA key for strength.

Concept level only: this page stops at point addition. Signing on a curve (ECDSA, Ed25519) adds a layer on top of exactly this arithmetic.

Toy numbers, never use these sizes for real.

Step 7 Why every real protocol uses both

Public-key maths is thousands of times slower than a symmetric cipher, so nothing encrypts bulk data with it. The asymmetric part runs once to agree on a symmetric key; AES carries everything after that.

Time to protect a payload

100 MB
key exchange (once)
AES for the bulk

Doing the whole payload with RSA instead would take about — illustrative desktop figures, not a benchmark.

Where to go next: /learn/https for the handshake that puts this together, /keypair to generate a real key pair, /aes to encrypt text with a symmetric key, and /cert to read what is actually inside a certificate.

Step 8 Glossary

TermIn one sentence
public keyThe half of a key pair you publish; it encrypts and it verifies signatures.
private keyThe half you never share; it decrypts and it creates signatures.
modulusThe number everything wraps around — n = p·q in RSA, the prime p in Diffie-Hellman.
modular exponentiationRaising a number to a power on a clock face; cheap forwards via square-and-multiply.
discrete logarithmThe reverse question — which exponent produced this value — with no known fast answer.
trapdoor / one-way functionEasy one way, hard the other, unless you hold the secret that opens the trapdoor.
key exchangeAgreeing on a shared symmetric key over a channel everyone can read (Diffie-Hellman).
digital signatureA value only the private key could produce and any public key holder can check.
hashA fixed-size fingerprint of a message; signatures sign the hash, not the message.
padding (OAEP / PSS)Structured randomness added before encrypting or signing, so textbook RSA leaks nothing.
elliptic curveA curve whose points form a group; the same one-way trick at a fraction of the key size.
hybrid encryptionPublic-key crypto to agree a key, symmetric crypto for the data.
post-quantumReplacement schemes built on lattices and hashes, because a large quantum computer would break RSA, Diffie-Hellman and elliptic curves alike.

The whole idea in one line: find a calculation that is easy forwards and hopeless backwards, publish the forward half, keep the backward half. Everything above is a different answer to that one question.

Send feedback

How this works

Stores only this report's id in your browser (localStorage) so a small note can pop up here once it ships. No account, nothing sent to us, no tracking. Clearing this site's data forgets it.

Changelog

What changed, and when. Newest first.

Loading…