Clever Quest Lab ← Labs

RSA Encryption

Build a key from two primes, encrypt and sign with it, then break a small key by factoring.

Key: –

Step 1 · Two different primes p and q

Type two primes, pick random ones, or load the classic example p = 61, q = 53, e = 17.

Step 2 · Modulus n and φ(n)

n = p · q
–
φ(n) = (p − 1)(q − 1)
–
λ(n) = lcm(p − 1, q − 1)
–

This page computes d modulo φ(n). Many real implementations use λ(n) instead, which gives a smaller d that works just as well (shown in step 4).

Step 3 · Public exponent e

Step 4 · Private exponent d = e−1 mod φ(n)

The extended Euclidean algorithm. Every row keeps r = s·φ(n) + t·e. A new row is the row two above minus q times the row above, where q is the quotient of their remainders. When r reaches 1, t is an inverse of e.

Your key pair

Public key (share it)

n =

e =

Private key (keep it secret)

d =

p and q must stay secret too: they give d at once.

Values kept for fast decryption (CRT)

Runs on your device. Nothing you type or record is uploaded. Keeps working without a connection while the page is open. Works with touch. Saves your primes p and q, the exponent e (under 420 digits) and the tab and display options on this device.

Added · Updated : the public factoring record is now RSA-896, 896 bits, factored in September 2026

How to use it

Work through the four tabs in order. Keys builds a key pair from two primes: type your own, press Random primes, or load the textbook example (p = 61, q = 53, e = 17). Encrypt & decrypt turns a number or a short text into ciphertext and back. Sign & verify uses the same maths the other way round, and Break & pitfalls factors small keys and shows why plain “textbook” RSA must never be used on its own. All arithmetic is exact whole-number arithmetic, so even 1024-bit primes give exact results.

This is a learning tool, not a way to protect real data: it adds no padding and keeps every key inside the page.

Controls

How it works

RSA was first described publicly in 1977 by Ron Rivest, Adi Shamir and Leonard Adleman and is named after their initials. Choose two different primes p and q and multiply them: n = p·q. Euler’s totient φ(n) = (p − 1)(q − 1) counts the numbers from 1 to n that share no factor with n. Pick a public exponent e with gcd(e, φ(n)) = 1, and the extended Euclidean algorithm finds the private exponent d with e·d ≡ 1 (mod φ(n)). The public key is (n, e); d, p and q stay secret.

c = me mod n    m = cd mod n

Why decryption works: e·d = 1 + k·φ(n), and Euler’s theorem says mφ(n) ≡ 1 (mod n) whenever m shares no factor with n, so cd = med = m·(mφ(n))k ≡ m. The Chinese remainder theorem extends this to every m from 0 to n − 1. The Carmichael function λ(n) = lcm(p − 1, q − 1) can replace φ(n); it usually gives a smaller d that works as well.

Powers are computed by square-and-multiply: read the exponent in binary, square at every bit after the first and also multiply by the base at every 1. A 2048-bit exponent needs only about 2048 squarings. To sign, the owner computes s = md mod n; anyone can check that se mod n = m with the public key.

Security rests on factoring: whoever finds p and q can compute φ(n) and then d. Trial division needs up to √n divisions, and Pollard’s rho roughly the fourth root of n steps when p and q have similar sizes, so both explode as keys grow. The general number field sieve, the fastest known method, does far better, yet the public record in October 2026 was RSA-896, an 896-bit challenge number factored in September 2026. That is why real keys have 2048 bits or more.

Things to try

Frequently asked questions

Why is a large prime only “probably prime”?

Miller–Rabin can prove a number composite, but not prime. A composite passes one round with probability at most 1/4, so 16 or 24 random rounds leave a negligible chance of error. Below 3.3 × 1024 the page uses 13 fixed bases, which is a proven test, and below 232 it uses trial division.

Why must p and q differ?

With p = q, n = p² and its square root reveals p at once. The formula φ(n) = (p − 1)(q − 1) would also be wrong.

Why does text need blocks?

RSA works on numbers smaller than n. The page turns text into UTF-8 bytes and packs as many bytes into each block as always stay below n. With n = 3233 that is a single byte per block.

Why is 65537 so common?

It is prime and equals 216 + 1. Its binary form has only two 1s, so encrypting takes 16 squarings and one multiplication.

Sources