Skip to content
Documentation Background

Cryptography math

This is an optional deep dive. Cryptography fundamentals explains what each primitive provides. Here are the mechanics, worked with numbers small enough to check by hand. Real keys are thousands of bits long, and real implementations add padding and other safeguards that these toy versions leave out. Do not use the toy forms for anything real.


RSA rests on a one-way fact: multiplying two large primes is easy, and recovering them from the product is not.

Key generation

  1. Choose two primes: p = 3, q = 11.
  2. Compute the modulus: n = p x q = 33.
  3. Compute phi = (p - 1)(q - 1) = 2 x 10 = 20.
  4. Choose a public exponent e with no common factor with phi: e = 3.
  5. Compute the private exponent d such that e x d = 1 (mod phi): d = 7, since 3 x 7 = 21 = 1 (mod 20).

Public key: (n, e) = (33, 3). Private key: d = 7 (with n).

Encrypt a message m = 4 with the public key: c = m^e mod n.

4^3 = 64
64 mod 33 = 31 -> ciphertext c = 31

Decrypt with the private key: m = c^d mod n.

31 = -2 (mod 33)
(-2)^7 = -128
-128 mod 33 = 4 -> plaintext m = 4

Anyone can compute c from the public key. Recovering m without d requires phi, which requires factoring n. For n = 33 that takes a second. For a 2048-bit n it is not feasible with known methods.

Sign by running the operation the other way, with the private key: s = m^d mod n.

4^7 mod 33: 4^2 = 16, 4^3 = 31, 4^4 = 25, 4^5 = 1, so 4^7 = 4^5 x 4^2 = 16
signature s = 16

Verify with the public key: s^e mod n must equal the message.

16^3 = 4096, 4096 mod 33 = 4 -> matches m, signature valid

The same pair of exponents serves both purposes, because (m^e)^d and (m^d)^e are both m mod n. Encrypting with the public key gives confidentiality. Operating with the private key gives a signature that anyone can check. In practice a signature is made over a hash of the message, and encryption wraps a symmetric session key rather than the message.


Diffie-Hellman lets two parties build a shared secret in public. It uses a public prime p and a public base g.

Take p = 23, g = 5.

StepParty AParty B
Choose a private numbera = 6b = 15
Compute the public value g^x mod pA = 5^6 mod 23 = 8B = 5^15 mod 23 = 19
Exchangesends 8sends 19
Compute the shared secretB^a = 19^6 mod 23 = 2A^b = 8^15 mod 23 = 2

Both get 2 because (g^b)^a and (g^a)^b are both g^(ab) mod p. An eavesdropper sees p, g, 8, and 19. To get the secret, they would have to find a from 5^a = 8 (mod 23), the discrete logarithm problem. For 23 you can try every exponent. For a 2048-bit prime, or a well-chosen elliptic curve, no efficient method is known.

Working one of the powers: 5^2 = 25 = 2, 5^4 = 4, 5^8 = 16, so 5^15 = 5^8 x 5^4 x 5^2 x 5 = 16 x 4 x 2 x 5 = 640, and 640 mod 23 = 19.

Ephemeral keys and forward secrecy. If both sides pick fresh a and b for each session and discard them, a later theft of a long-term key reveals nothing about past sessions. That is the property TLS 1.3 requires. Plain DH has no authentication. An attacker in the middle can run one exchange with each side. That is why the exchange is signed by a certificate key, or tied to a known identity, in TLS, SSH, and IKE.


A block cipher turns a fixed-size block of plaintext into a block of ciphertext under a key. Encrypting each block independently (ECB mode) has a visible flaw: identical plaintext blocks give identical ciphertext blocks, so patterns in the data survive encryption. An encrypted image in ECB mode still shows its outline.

Cipher block chaining removes that by mixing the previous ciphertext block into each new one:

C0 = IV
Ci = Encrypt( key, Pi XOR C(i-1) ) (encrypt)
Pi = Decrypt( key, Ci ) XOR C(i-1) (decrypt)

With chaining, two equal plaintext blocks at different positions produce different ciphertext, since their predecessors differ. The first block has no predecessor, so an initialization vector (IV) stands in. Rules for the IV:

  • It need not be secret. It is sent with the message.
  • It must be unpredictable and different for every message encrypted under the same key. A reused or predictable IV leaks whether two messages start the same way, and enables attacks on some protocols.
  • Generating a fresh random IV per message costs one extra block of overhead.

Properties to know:

PropertyConsequence
Encryption is sequentialCannot be parallelized, which slows high-speed use
Decryption can be parallelEach plaintext block needs only two ciphertext blocks
A flipped bit in Ci scrambles Pi and flips the same bit in P(i+1)An attacker who can modify ciphertext can make targeted changes to the next block. Encryption alone does not protect integrity
Padding is required to fill the last blockCareless padding error handling enabled the padding oracle attacks

Because of those last two rows, modern protocols use authenticated encryption (AES-GCM, ChaCha20-Poly1305), which combines confidentiality with an integrity tag. CBC is mainly seen in older protocol versions. See the mode table in cryptography fundamentals.


An elliptic curve group replaces “multiply numbers modulo p” with “add points on a curve”. The one-way operation is repeating point addition: given a base point G and a number k, computing Q = kG is fast, and recovering k from Q and G is the elliptic curve discrete logarithm problem. The best known attacks on it are much less effective than those on ordinary discrete logarithms, so smaller keys give the same strength:

Security levelRSA or DH modulusElliptic curve key
About 112 bits2048 bits224 bits
About 128 bits3072 bits256 bits
About 192 bits7680 bits384 bits

Smaller keys mean faster handshakes, smaller certificates, and lower cost on constrained devices. Diffie-Hellman over a curve is ECDH, and the signature scheme is ECDSA, or EdDSA with the Ed25519 curve. Curves like X25519 and Ed25519 are the defaults in modern SSH, TLS, and WireGuard.


Quantum computers change the threat in two different ways.

Algorithm familyEffect of a large quantum computerResponse
RSA, Diffie-Hellman, elliptic curvesBroken. Shor’s algorithm solves factoring and discrete logarithms in polynomial time. Larger keys do not helpReplace with post-quantum algorithms
Symmetric ciphers (AES)Weakened. Grover’s algorithm square-roots the brute-force search, so a 128-bit key behaves like 64 bits and a 256-bit key like 128 bitsPrefer 256-bit keys
Hashes (SHA-256, SHA-3)Collision and preimage resistance reduced, but still practical at 256 bitsNo urgent change

A practical consequence is harvest now, decrypt later: an adversary can record encrypted traffic today and decrypt it once a capable machine exists. Data that must stay secret for decades needs protection now, and key exchange is the exposed part.

Post-quantum cryptography uses mathematical problems believed to resist quantum attack. NIST standardized lattice-based algorithms in 2024, for key encapsulation (ML-KEM, derived from Kyber) and signatures (ML-DSA, derived from Dilithium), plus a hash-based signature scheme (SLH-DSA). Deployments typically run a hybrid key exchange, combining a classical curve such as X25519 with a post-quantum KEM, so the result is safe if either one holds. Several mainstream browsers and TLS libraries already negotiate hybrid groups.

Practical steps for operators: inventory where RSA and elliptic-curve keys are used (TLS, VPNs, SSH, code signing, certificates), keep libraries current so hybrid groups can be enabled, and favor crypto-agility, meaning algorithms can be swapped without redesigning systems.