Guided Lab  |  Your Own Keys  |  Encrypt Your Own Message

Build RSA by Hand

Don't just read about RSA — build it. Pick your own two primes, compute the modulus and totient yourself, choose a public exponent, watch your private key get derived, then encrypt and decrypt your own short message with the keys you just built. Every step is checked as you go — if a computation is wrong, you'll find out immediately, with a hint toward the fix.

Your Keychain: p=? q=? n=? φ(n)=? e=? d=?
1
Choose Two Primes
p and q must be different primes — these are the secret starting point of your key pair

2
Compute the Modulus n
n = p × q — this becomes part of both your public and private key

3
Compute Euler's Totient φ(n)
φ(n) = (p−1) × (q−1) — counts the numbers below n that share no factor with n

4
Choose the Public Exponent e
Pick any e where 1 < e < φ(n) and gcd(e, φ(n)) = 1 — this becomes part of your public key

5
Derive the Private Exponent d
d is the modular inverse of e mod φ(n) — this is the secret half of your key pair

6
Encrypt a Message with Your Public Key (e, n)
Letters only, up to 8 characters — each letter (A=0 ... Z=25) is encrypted separately as c = m^e mod n

7
Decrypt with Your Private Key (d, n)
m = c^d mod n — should recover your original message exactly

What You Just Built

RSA key generation is exactly the seven steps above, and nothing more: pick two primes, multiply them, compute the totient, choose a public exponent coprime to the totient, derive its modular inverse as the private exponent, and you have a complete key pair. Real-world RSA uses primes hundreds of digits long instead of two small ones, purely so that factoring n back into p and q is computationally infeasible — the algorithm itself is identical to what you just did by hand.

Why Each Step Matters

StepPurpose
Choosing p, qTheir product n is public, but n's factorization must stay secret — this is RSA's entire security foundation
Computing φ(n)Defines the group structure that makes encryption and decryption mathematical inverses of each other
Choosing e coprime to φ(n)Guarantees e has a modular inverse — without this, step 5 would be impossible
Deriving dd undoes exactly what e does, because d and e are modular inverses mod φ(n) — this is the mathematical guarantee that decryption recovers the original message

Why Real RSA Uses Enormous Primes

The security of everything you just built rests entirely on one fact: given n, finding p and q is hard. With n around 150 to 900 (as in this lab), factoring it takes a computer microseconds. Real RSA uses primes hundreds of digits long specifically so that n becomes computationally infeasible to factor with any known classical algorithm — at least until a large enough quantum computer runs Shor's algorithm, which factors n in polynomial time regardless of size. That's the entire reason NIST standardized ML-DSA (Dilithium) and ML-KEM (Kyber): they don't rely on factoring at all, so Shor's algorithm doesn't apply to them.

Want to see that comparison directly? Try the Quantum Threat Simulator, or practice the underlying operations more with the Modular Arithmetic Trainer.

References

  1. RSA (cryptosystem) — general background
  2. NIST FIPS 203 — ML-KEM Standard (2024)
  3. KF-Cipher Modular Arithmetic Trainer