A four-part explainer: the mathematics behind public-key cryptography with small worked examples, then how SSH, OpenPGP, TLS, WireGuard, FIDO2 and Signal use it.

13 notes
Clock arithmetic — exercises Practise modular arithmetic step by step: remainders, negative numbers, inverses, the extended Euclidean algorithm, solving equations, Fermat's little theorem, and where you already use it (weekdays, IBAN, ISBN).Nov 29, 2026
Diffie–Hellman — exercises and in depth Play both sides of a Diffie–Hellman exchange, see why the group must have a large prime order, run a man-in-the-middle attack and a small-subgroup attack with real numbers, take a discrete log apart with Pohlig–Hellman, and see which groups real protocols use.Nov 29, 2026
Digital signatures — exercises and in depth Sign and verify with Schnorr and ECDSA on a small curve, see why Schnorr is secure (identification and Fiat–Shamir), extract a private key from a reused nonce in both schemes, and see how Ed25519 makes that impossible.Nov 29, 2026
Easy forward, hard backward — exercises Practise the one-way functions behind public-key cryptography: square-and-multiply by hand, discrete logarithms, generators, baby-step giant-step, and factoring (and why primes that are close together are a disaster).Nov 29, 2026
Elliptic curves and ECDH — the mathematics in depth Derive the point-addition formulas yourself, find every point on a curve by hand, compute with double-and-add, do ECDH on two small curves, and see how Curve25519, X25519, the Montgomery ladder and Edwards curves turn this into the key exchange in every SSH and HTTPS connection.Nov 29, 2026
Prime numbers — the atoms of cryptography What prime numbers are, why there are infinitely many, how many there are, Euler's φ and Fermat's little theorem with proofs, how computers test whether a 300-digit number is prime, and the special primes used in real cryptography. With exercises.Nov 29, 2026
Public-key cryptography 1 — The mathematics Clock arithmetic, RSA, Diffie–Hellman, elliptic curves and digital signatures, each worked out with small numbers you can check by hand.Nov 29, 2026
Public-key cryptography 2 — How an SSH connection is built A real SSH connection step by step: algorithm negotiation, the (post-quantum) key exchange, the host key signature, known_hosts, session keys and how your own key proves who you are.Nov 29, 2026
Public-key cryptography 3 — How OpenPGP (GnuPG) encrypts and signs Encryption without a live connection: primary keys and subkeys, the random session key, ECDH with a throwaway key, signatures, fingerprints and the web of trust, with real gpg packet output.Nov 29, 2026
Public-key cryptography 4 — TLS, WireGuard, FIDO2 and Signal Four more ways to combine the same mathematics: certificates in HTTPS, signature-free authentication in WireGuard, phishing-proof logins with FIDO2 and passkeys, and keys that change with every message in Signal.Nov 29, 2026
Public-key cryptography 5 — S/MIME: email encryption with certificates How S/MIME signs and encrypts email, the mathematics inside it, where to get a certificate (free, paid, or through your university), how to make your own key with OpenSSL, and how to set it up in Thunderbird, Outlook, Zimbra and Evolution.Nov 29, 2026
RSA — exercises and in depth Build RSA keys by hand, encrypt, decrypt and sign, see the full proof of why it works, speed it up with the Chinese remainder theorem, break textbook RSA in five different ways, and read a real OpenSSL key.Nov 29, 2026
Why 77 to 925 digits? — how big is big enough Where the 77 digits of Curve25519 and the 925 digits of an RSA-3072 key come from, what 128-bit security means, why RSA needs twelve times more digits than an elliptic curve, why we don't simply use bigger numbers, and what quantum computers change.Nov 29, 2026
README.md PDF

🔑 Public-key cryptography: from the mathematics to SSH, PGP and HTTPS

How can two computers that have never met agree on a secret while everybody is listening? How does a server prove that it is really the server? And what does your SSH key actually do when you log in?

Most explanations online give you either the formulas without the protocols, or the commands without the mathematics. This series connects the two. Part 1 builds the mathematics with small numbers you can check by hand. The other parts follow real connections, with real output, and point at the exact formula from part 1 that is doing the work.


📚 Parts

#PartWhat you learn
1The mathematicsClock arithmetic, one-way functions, RSA, Diffie–Hellman, elliptic curves, signatures, key sizes and quantum computers
2How an SSH connection is builtA real ssh -vv session step by step: negotiation, post-quantum key exchange, host key, known_hosts, session keys, your key
3How OpenPGP encrypts and signsEncryption without a live connection: subkeys, the session key, ECDH with a throwaway key, signatures, fingerprints, trust
4TLS, WireGuard, FIDO2 and SignalCertificates, Diffie–Hellman without signatures, phishing-proof logins, and keys that change with every message
5S/MIME: email encryption with certificatesHow S/MIME signs and encrypts mail, the mathematics inside, where to get a certificate, and setting it up in Thunderbird, Outlook, Zimbra and Evolution

✏️ Practice and deep dives for part 1

PageWhat you practise
Clock arithmeticRemainders, inverses, the extended Euclidean algorithm, Fermat’s little theorem, IBAN and ISBN
Easy forward, hard backwardSquare-and-multiply, discrete logarithms, generators, baby-step giant-step, factoring
Prime numbersEuclid’s proof, the prime number theorem, Euler’s φ, Fermat with proof, Miller–Rabin, special primes
RSAYour own keys, the proof, the CRT speed-up, five attacks on textbook RSA, a real OpenSSL key
Diffie–HellmanSubgroups, Pohlig–Hellman, small-subgroup and man-in-the-middle attacks, forward secrecy
Elliptic curves and ECDHDeriving the formulas, all points by hand, double-and-add, Curve25519, the Montgomery ladder, Edwards curves
Digital signaturesSchnorr and Fiat–Shamir, ECDSA by hand, recovering a key from a reused nonce, Ed25519
Why 77 to 925 digits?Bits and digits, what 128-bit security means, why RSA needs bigger numbers than curves, records, quantum computers

Read part 1 first; parts 2 to 5 can be read in any order.


🧭 The whole story in five lines

  1. Some calculations are easy forward and practically impossible backward: multiplying primes (RSA), and repeated multiplication on a clock or on an elliptic curve (Diffie–Hellman, Curve25519).
  2. Diffie–Hellman lets two sides create a shared secret that never travels over the line.
  3. Signatures prove who took part in that exchange, without revealing the private key.
  4. The shared secret is turned into keys for a fast symmetric cipher (AES, ChaCha20), which encrypts the actual data.
  5. Every protocol then has to answer one question that the mathematics cannot: whose key is this? (known_hosts, fingerprints, certificate authorities, a config file, a safety number).