TExcelTAD
Home › Number Theory & Cryptography › RSA Key Generation

RSA Key Generation

RSA (Rivest, Shamir, Adleman, 1977) was the first practical public-key cryptosystem and is still used for signatures and key transport. Its key generation is a tour of the course so far: random primes, Euler's or Carmichael's function, a gcd check and a modular inverse.

The recipe

  1. Choose two random primes p≠qp \ne q of equal size (1024 bits each for 2048-bit RSA).
  2. Compute n=pqn = pq (the modulus) and λ(n)=lcm⁡(p−1,q−1)\lambda(n) = \operatorname{lcm}(p - 1, q - 1) (or φ(n)=(p−1)(q−1)\varphi(n) = (p - 1)(q - 1)).
  3. Choose the public exponent ee with gcd⁡(e,λ(n))=1\gcd(e, \lambda(n)) = 1. Almost everyone uses e=65537e = 65537.
  4. Compute the private exponent d=e−1 mod λ(n)d = e^{-1} \bmod \lambda(n).

Public key: (n,e)(n, e). Private key: dd (plus pp, qq and the CRT values for speed).

Encryption is c=me mod nc = m^e \bmod n and decryption is m=cd mod nm = c^d \bmod n; correctness is Euler's theorem.

python · try it yourself
from math import gcd, lcm
from sympy import nextprime

# Toy 128-bit primes from fixed starting points (real keys use unpredictable 1024+-bit primes)
p = nextprime(0xC3A5_9F12_7E44_0B8D_19A2_77C0_5D3E_F001)
q = nextprime(0xE1F0_3B6C_A925_D47E_8B01_C3F2_6A78_9002)
n = p * q
lam = lcm(p - 1, q - 1)
e = 65537
assert gcd(e, lam) == 1
d = pow(e, -1, lam)
print("n bits:", n.bit_length())
print("e * d mod lambda:", e * d % lam)
m = int.from_bytes(b"hello RSA", "big")
c = pow(m, e, n)
print("decrypts:", pow(c, d, n).to_bytes(9, "big"))
n bits: 256
e * d mod lambda: 1
decrypts: b'hello RSA'

Why e=65537e = 65537?

  • It is prime, so gcd⁡(e,λ(n))=1\gcd(e, \lambda(n)) = 1 unless ee divides p−1p - 1 or q−1q - 1, which key generators simply avoid.
  • It has only two 1-bits, so encryption and signature verification cost 17 multiplications (Fast Modular Exponentiation).
  • It is large enough to avoid the attacks on e=3e = 3 with poor padding.

What must stay secret

Anyone who learns any one of pp, qq, φ(n)\varphi(n), λ(n)\lambda(n) or dd can compute all the others. Knowing dd gives a multiple of λ(n)\lambda(n), namely ed−1ed - 1, and that is enough to factor nn with a randomized algorithm that looks for a non-trivial square root of 1 (the same trick as in Miller-Rabin).

python · try it yourself
from math import gcd

def factor_from_d(n, e, d):
    k = e * d - 1                       # a multiple of lambda(n)
    t, s = k, 0
    while t % 2 == 0:
        t, s = t // 2, s + 1
    for g in range(2, 100):
        x = pow(g, t, n)
        for _ in range(s):
            y = x * x % n
            if y == 1 and x not in (1, n - 1):
                return gcd(x - 1, n)
            x = y
    return None

p, q, e = 1000003, 1000033, 65537
n = p * q
d = pow(e, -1, (p - 1) * (q - 1))
f = factor_from_d(n, e, d)
print(f, n // f)
1000003 1000033

Consequence: never share a modulus between users, even with different exponents. Each user's (e,d)(e, d) factors the common nn.

Key sizes

Modulus Security Status
512 bits none (factored in hours) broken
1024 bits about 80 bits deprecated
2048 bits about 112 bits minimum today
3072 bits about 128 bits recommended for long-term use
4096 bits about 140 bits common for root CAs

In software

  • openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072, Python cryptography's rsa.generate_private_key(public_exponent=65537, key_size=3072).
  • The private-key file (PKCS #1 / PKCS #8) stores n,e,d,p,q,d mod (p−1),d mod (q−1),q−1 mod pn, e, d, p, q, d \bmod (p - 1), d \bmod (q - 1), q^{-1} \bmod p: the extra numbers are for CRT decryption.

Exercises

Exercise 1: Tiny key by hand

With p=5p = 5, q=11q = 11 and e=3e = 3, compute nn, φ(n)\varphi(n) and dd (use φ\varphi). Encrypt m=9m = 9.

Answer

n=55n = 55, φ=40\varphi = 40, d=27d = 27, c=14c = 14.

Solution

n=55n = 55, φ(n)=4⋅10=40\varphi(n) = 4 \cdot 10 = 40, gcd⁡(3,40)=1\gcd(3, 40) = 1, and 3⋅27=81=2⋅40+13 \cdot 27 = 81 = 2 \cdot 40 + 1, so d=27d = 27. 93=729=13⋅55+149^3 = 729 = 13 \cdot 55 + 14, so c=14c = 14. Check decryption with pow(14, 27, 55), which returns 9.

Exercise 2: A bad exponent

With p=7p = 7 and q=13q = 13, why is e=3e = 3 not allowed?

Answer

gcd⁡(3,λ(91))=gcd⁡(3,12)=3\gcd(3, \lambda(91)) = \gcd(3, 12) = 3.

Solution

λ(91)=lcm⁡(6,12)=12\lambda(91) = \operatorname{lcm}(6, 12) = 12 (and φ=72\varphi = 72). Both are divisible by 3, so 3 has no inverse and decryption is impossible: m↦m3m \mapsto m^3 is not even one-to-one modulo 91.

Exercise 3: φ\varphi versus λ\lambda (code)

For p=1000003p = 1000003, q=1000033q = 1000033 and e=65537e = 65537, compute dd with φ(n)\varphi(n) and with λ(n)\lambda(n) and check that both decrypt.

Answer

Different values of dd, both correct.

Solution
python · try it yourself
from math import lcm
p, q, e = 1000003, 1000033, 65537
n = p * q
d_phi = pow(e, -1, (p - 1) * (q - 1))
d_lam = pow(e, -1, lcm(p - 1, q - 1))
c = pow(424242, e, n)
print(d_phi, d_lam, pow(c, d_phi, n), pow(c, d_lam, n))
983264276609 149902609889 424242 424242

Any dd with ed≡1(modλ(n))ed \equiv 1 \pmod{\lambda(n)} works; the λ\lambda version is the smallest.

Test yourself

In RSA, the public key is…

  1. (p,q)(p, q)
  2. (n,e)(n, e)
  3. (n,d)(n, d)
  4. φ(n)\varphi(n)

The private exponent dd is…

  1. e−1 mod ne^{-1} \bmod n
  2. e−1 mod λ(n)e^{-1} \bmod \lambda(n) (or mod φ(n)\varphi(n))
  3. n−en - e
  4. a random number

Why must two users never share an RSA modulus?

  1. it wastes memory
  2. either user's key pair lets them factor nn and compute the other's dd
  3. it changes ee
  4. it is slower