RSA

Work in progress. This note is still being written and incomplete.

Named after Rivest, Shamir, Adleman. A block cipher over Zn\mathbb{Z}_n used for both encryption and digital signatures. Security rests on the integer factorization problem: given n=pqn = pq for 2 large equal-size primes, finding p,qp, q is computationally intractable. The interactive guide to RSA steps through key generation, encryption, and why factoring nn is hard.

Key Pair Generation

  • Choose 2 large, distinct, equal-size primes p,qp, q, kept secret.
  • Compute n=pqn = pq, public, its bit length is the RSA key length.
  • Compute ϕ(n)=(p1)(q1)\phi(n) = (p-1)(q-1), kept secret.
  • Choose ee with 1<e<ϕ(n)1 < e < \phi(n) and gcd(e,ϕ(n))=1\gcd(e, \phi(n)) = 1, public.
  • Compute de1(modϕ(n))d \equiv e^{-1} \pmod{\phi(n)}, secret.

Public key: {e,n}\{e, n\}. Private key: {d}\{d\}. pp, qq, and ϕ(n)\phi(n) must be destroyed after generation.

Primality Testing

pp and qq are found by drawing random odd integers of the target bit length and testing each for primality until one passes.

  • Trial division by the first few hundred small primes rejects most candidates in a few operations.
  • Survivors go to the Miller-Rabin probabilistic test.

Miller-Rabin tests an odd n>2n > 2. Write n1=2smn - 1 = 2^s m with mm odd. Pick a random base aa with 2an22 \le a \le n - 2. Then nn passes base aa iff am1(modn)a^m \equiv 1 \pmod n or a2rm1(modn)a^{2^r m} \equiv -1 \pmod n for some rr with 0r<s0 \le r < s.

A composite nn passes at most 1/41/4 of the bases. Running kk independent random bases bounds the probability of accepting a composite by 4k4^{-k}. kk around 4040 is standard.

Each base costs 1 modular exponentiation, O ⁣((logn)3)O\!\left((\log n)^3\right) bit operations with schoolbook multiplication. So a candidate is classified in time polynomial in the key length.

Trace for n=221n = 221, base a=137a = 137:

  • n1=220=2255n - 1 = 220 = 2^2 \cdot 55, so s=2s = 2, m=55m = 55.
  • ammodn=13755mod221=188a^m \bmod n = 137^{55} \bmod 221 = 188, not 11 and not 220220.
  • r=1r = 1: 1882mod221=205188^2 \bmod 221 = 205, not 220220.
  • No residue reached 1-1, so 137137 is a witness and 221221 is composite.

Indeed 221=13×17221 = 13 \times 17.

Encryption and Decryption

c=memodn,m=cdmodnc = m^e \bmod n, \quad m = c^d \bmod n

Encryption cost is a single exponentiation with a small exponent ee. Decryption cost is a single exponentiation with an exponent up to the full length of nn, far more expensive.

Hybrid Encryption

RSA is not used to encrypt large data directly, since modular exponentiation on large numbers is slow and the message must be smaller than nn. Instead:

  • The sender picks a random symmetric session key kk and encrypts the file with a symmetric cipher such as AES under kk.
  • The sender encrypts kk with the receiver’s RSA public key and sends both the encrypted file and the encrypted key.
  • The receiver recovers kk with their RSA private key, then decrypts the file.

Correctness

(memodn)dmodn=m(m^e \bmod n)^d \bmod n = m

Worked Example

p=17p = 17, q=11q = 11.

  • n=187n = 187.
  • ϕ(n)=16×10=160\phi(n) = 16 \times 10 = 160.
  • e=7e = 7, since gcd(7,160)=1\gcd(7, 160) = 1.
  • d=23d = 23, since 23×7=161=160+123 \times 7 = 161 = 160 + 1.
  • Public key {7,187}\{7, 187\}, private key {23}\{23\}.
  • For m=88m = 88: c=887mod187=11c = 88^7 \bmod 187 = 11, and m=1123mod187=88m = 11^{23} \bmod 187 = 88.
Written by September 16, 2026 3 min read
Was this helpful?