Skip to content
CryptographyInternals

08 / 11 · Modern cryptography

RSA

Easy to multiply two primes, astronomically hard to split the result.

Published
1977, Rivest, Shamir & Adleman
Hard problem
Integer factorization
Used in
Certificates, signatures, SSH keys

RSA was the first practical public-key system: anyone can encrypt with your public key or check your signature, but only you, holding the private key, can decrypt or sign.

Its security rests on a simple asymmetry. Multiplying two large primes is instant. Given only their product, finding the primes again is, as far as anyone knows, infeasible.

Primes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
p = 5q = 1117 primes ≤ 60
How it works
11 ÷ 2 →r 111 ÷ 3 →r 2⇒11

no divisor up to √11 ≈ 3.3: prime

01 / 14 · Inside RSA

Two secret primes

RSA starts with two primes, numbers divisible only by 1 and themselves. Here p = 5 and q = 11; a real key uses two primes about 309 digits long.

Everything secret in RSA comes from these two numbers: whoever learns p or q can rebuild the private key.

The basics: Prime numbers →

Playground

Build your own RSA key, use it, then break it

Pick two primes and a public exponent, encrypt a number, and step through every calculation on your values: n, φ(n), d by extended Euclid, square-and-multiply, decryption, and the attack that factors n.

Your input

Primes below 100 000 keep every step readable.

Must share no factor with φ(n).

n
3233
φ(n)
3120
d
c
Primes
pprime
61
÷2 r1÷3 r1÷5 r1÷7 r5
qprime
53
÷2 r1÷3 r2÷5 r3÷7 r4

Primes

Two secret primes

What happens

RSA starts from two primes, p and q: numbers divisible only by 1 and themselves. To check one, try every divisor up to its square root: a bigger divisor would pair with a smaller one already tried.

On your values

p = 61: ÷ 2, 3, 5, 7 → none divides it · primetried 4 divisors up to √61 ≈ 7.8

q = 53: ÷ 2, 3, 5, 7 → none divides it · primetried 4 divisors up to √53 ≈ 7.3

Why it matters

The formula φ(n) = (p − 1)(q − 1) that gives the private key only holds for primes. With a composite number, decryption breaks and n has more small factors for an attacker to find.

The basics: Prime numbers →

Start

Primes

Break

Now race your browser against bigger keys

Your browser generates keys of growing size and factors each n by trial division. The times are measured, then extrapolated to real key sizes.

Trial division doubles in cost every 2 bits. Better algorithms exist (the number field sieve), but even with them the public record is RSA-250, 829 bits, and real keys use 2048 bits or more.

How it works

  1. 1Pick two large primes p and q, and compute n = p × q.
  2. 2Compute φ(n) = (p − 1)(q − 1).
  3. 3Choose a public exponent e coprime to φ(n), usually 65537, and compute the private exponent d = e⁻¹ mod φ(n).
  4. 4Encrypt: c = mᵉ mod n. Decrypt: m = cᵈ mod n. Publish (n, e) and keep d secret.

Why it holds

Factoring is hard
Computing d requires φ(n), which requires p and q. The best known method, the general number field sieve, grows faster than any polynomial in the size of n.
Real-world records
The largest RSA number factored publicly is RSA-250 (829 bits), in 2020, at a cost of about 2,700 CPU core-years. 2048-bit RSA is estimated to be billions of times harder.
Padding
Real RSA never encrypts raw numbers. OAEP padding adds randomness, so encrypting the same message twice gives different ciphertexts.

What can go wrong

Textbook RSA is broken
Without padding, RSA is deterministic and malleable. Small exponents, shared primes or bad randomness have broken real keys.
Quantum computers
Shor's algorithm factors in polynomial time on a large enough quantum computer. None exists yet, but data recorded today could be decrypted later, so migration to post-quantum signatures has started.
Read the in-depth articleRSA Encryption Example Step by Step (With Small Numbers)A full RSA example with small numbers: p = 61, q = 53, e = 17, d = 2753. Encrypt 65 to 2790 and back, with every step of the math shown and checked.