Skip to content

Number Theory and Cryptography

Classical ciphers share one key: anyone who can encrypt can decrypt. Public-key cryptography splits the key: a public key (published, used to encrypt or verify) and a private key (secret, used to decrypt or sign). The trick is a one-way function with a trapdoor: easy to compute in one direction, computationally infeasible to invert without secret knowledge.

Number theory supplies both halves:

  • Easy: multiply two large primes; compute modular powers by repeated squaring.
  • Hard: factor a 600-digit composite; compute discrete logarithms modulo a large prime.

Setup. Choose two large distinct primes p,qp, q (each hundreds of digits). Let n=pqn = pq and φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1). Choose ee with gcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1; compute the private exponent dd with ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)} (extended Euclid). Public key: (n,e)(n, e). Private key: dd.

Encryption of a message mm (0≤m<n0 \leq m < n): c≡me(modn)c \equiv m^e \pmod n.

Decryption: m≡cd(modn)m \equiv c^d \pmod n.

Why it works. Euler’s theorem: since gcd⁡(m,n)=1\gcd(m, n) = 1 (for mm coprime to nn),

cd≡(me)d=med≡m(modn),c^d \equiv (m^e)^d = m^{ed} \equiv m \pmod n,

because ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)} implies ed=1+kφ(n)ed = 1 + k\varphi(n) and med=m⋅(mφ(n))k≡mm^{ed} = m \cdot (m^{\varphi(n)})^k \equiv m. (A sharper proof works for all mm using med≡m(modp)m^{ed} \equiv m \pmod p and (modq)\pmod q and CRT.)

Worked example (toy size). p=61p = 61, q=53q = 53, n=3233n = 3233, φ(n)=60⋅52=3120\varphi(n) = 60 \cdot 52 = 3120, e=17e = 17, d=2753d = 2753 (since 17⋅2753=46801=15⋅3120+117 \cdot 2753 = 46801 = 15 \cdot 3120 + 1). Encrypt m=65m = 65: c=6517 mod 3233=2790c = 65^{17} \bmod 3233 = 2790. Decrypt: 27902753 mod 3233=652790^{2753} \bmod 3233 = 65 ✓ (by repeated squaring: 12 squarings and a handful of multiplications).

Security. Recovering dd from (n,e)(n, e) is as hard as factoring nn (there are reductions both ways for proper key generation). All classical attacks reduce to factoring; 2048-bit nn is the current floor. Pitfall: shared primes, small ee without padding, and deterministic encryption are the standard exam criticisms.

Public: a large prime pp and a primitive root gg modulo pp.

  1. Alice chooses secret aa; publishes A=ga mod pA = g^a \bmod p.
  2. Bob chooses secret bb; publishes B=gb mod pB = g^b \bmod p.
  3. Shared secret: Alice computes K=Ba≡gabK = B^a \equiv g^{ab}; Bob computes K=Ab≡gabK = A^b \equiv g^{ab} (both reduced mod pp).

An eavesdropper sees p,g,ga,gbp, g, g^a, g^b but must solve the discrete logarithm problem to get aa or bb; infeasible for 2048-bit pp.

Worked example. p=23p = 23, g=5g = 5 (primitive root mod 23). Alice: a=6a = 6, A=56=15625≡8(mod23)A = 5^6 = 15625 \equiv 8 \pmod{23}. Bob: b=15b = 15, B=515≡19(mod23)B = 5^{15} \equiv 19 \pmod{23}. Shared: K=196≡2(mod23)=815(mod23)K = 19^6 \equiv 2 \pmod{23} = 8^{15} \pmod{23} ✓.

Public: p,gp, g as above and Alice’s public key A=gaA = g^a. To encrypt mm to Alice: pick ephemeral kk, send c1=gkc_1 = g^k and c2=m⋅Ak mod pc_2 = m \cdot A^k \bmod p. Alice decrypts with her secret aa:

m=c2⋅(c1a)−1(modp),m = c_2 \cdot (c_1^a)^{-1} \pmod p,

using c1a=gka=Akc_1^a = g^{ka} = A^k. Security rests on the Diffie–Hellman problem.

  • Repeated squaring computes me mod nm^e \bmod n in O(log⁡e)O(\log e) multiplications; the reason large exponents are feasible.
  • gcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1 guarantees dd exists (multiplicative inverse modulo φ(n)\varphi(n), extended Euclid; see Congruences).
  • Decryption works for all mm (not just coprime ones) when n=pqn = pq: check med≡mm^{ed} \equiv m mod pp and mod qq separately, then CRT.
  • φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1) requires p≠qp \neq q: with p=qp = q, φ(n)=p(p−1)\varphi(n) = p(p-1), not (p−1)2(p-1)^2.
  • Never publish dd or φ(n)\varphi(n): knowing φ(n)\varphi(n) is equivalent to knowing pp and qq.
  • Textbook RSA is deterministic and malleable: real systems use randomised padding (OAEP); state this in “criticise the scheme” questions.
  • Diffie–Hellman without authentication is a man-in-the-middle target: the fix is signatures (RSA or ElGamal signatures), not a change of group.
med≡m(modn)whenever ed≡1(modφ(n))m^{ed} \equiv m \pmod n \quad \text{whenever } ed \equiv 1 \pmod{\varphi(n)}

A full exam question walks the entire key generation: choose primes, compute nn and φ(n)\varphi(n), pick ee and invert it, encrypt a small message by repeated squaring, decrypt, and then criticise the scheme. Practise each stage to the point where repeated squaring is mechanical.

The standard criticism list: deterministic encryption leaks equality of plaintexts; small decryption exponents fall to Wiener’s continued-fraction attack (the continued-fraction page is the natural companion); shared factors between two public moduli break both keys; the factoring is a single gcd.