🔑 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
| # | Part | What you learn |
|---|---|---|
| 1 | The mathematics | Clock arithmetic, one-way functions, RSA, Diffie–Hellman, elliptic curves, signatures, key sizes and quantum computers |
| 2 | How an SSH connection is built | A real ssh -vv session step by step: negotiation, post-quantum key exchange, host key, known_hosts, session keys, your key |
| 3 | How OpenPGP encrypts and signs | Encryption without a live connection: subkeys, the session key, ECDH with a throwaway key, signatures, fingerprints, trust |
| 4 | TLS, WireGuard, FIDO2 and Signal | Certificates, Diffie–Hellman without signatures, phishing-proof logins, and keys that change with every message |
| 5 | S/MIME: email encryption with certificates | How 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
| Page | What you practise |
|---|---|
| Clock arithmetic | Remainders, inverses, the extended Euclidean algorithm, Fermat’s little theorem, IBAN and ISBN |
| Easy forward, hard backward | Square-and-multiply, discrete logarithms, generators, baby-step giant-step, factoring |
| Prime numbers | Euclid’s proof, the prime number theorem, Euler’s φ, Fermat with proof, Miller–Rabin, special primes |
| RSA | Your own keys, the proof, the CRT speed-up, five attacks on textbook RSA, a real OpenSSL key |
| Diffie–Hellman | Subgroups, Pohlig–Hellman, small-subgroup and man-in-the-middle attacks, forward secrecy |
| Elliptic curves and ECDH | Deriving the formulas, all points by hand, double-and-add, Curve25519, the Montgomery ladder, Edwards curves |
| Digital signatures | Schnorr 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
- 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).
- Diffie–Hellman lets two sides create a shared secret that never travels over the line.
- Signatures prove who took part in that exchange, without revealing the private key.
- The shared secret is turned into keys for a fast symmetric cipher (AES, ChaCha20), which encrypts the actual data.
- 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).
Related notes
- How a TOTP code is calculated: the same kind of hashing, for authenticator apps
- Key derivation functions: from a password or a shared secret to keys
- SSH notes and SSH server hardening
- YubiKey on Linux: keeping the private keys from this series in hardware
- Contact: how to send me something confidential