๐Ÿ” Interactive Simulation

RSA Encryption

Walk through every step of the world's most widely-used public-key cryptosystem โ€” generate real keys, encrypt a message, and decrypt it back.

๐Ÿงฎ
Step 1 โ€” Choose Two Prime Numbers (p and q)
RSA begins with two large secret prime numbers. Their product becomes the public modulus n. Here we use small primes so you can follow the math.
Why primes? Multiplying two large primes is trivially fast, but factoring their product back is computationally infeasible for large enough numbers. This asymmetry is RSA's mathematical trapdoor.
p
โ€”
ร—
q
โ€”
=
n = p ร— q (modulus)
โ€”
๐Ÿ—๏ธ
Step 2 โ€” Key Generation
From p and q we derive a public key (e, n) anyone can use to encrypt, and a private key (d, n) only you can use to decrypt.
RSA uses exponents e and d such that Meยทd โ‰ก M (mod n). Finding d from e requires knowing ฯ†(n), which requires knowing p and q.
Requirements for e
โœ“  1 < e < ฯ†(n)
โœ“  gcd(e, ฯ†(n)) = 1 (coprime)
โœ“  Typically 65537 in real RSA

Public Key โ€” share freelyPublic
e =โ€”
n =โ€”
Anyone uses (e, n) to encrypt messages to you.
Private Key โ€” keep secretSecret
d =โ€”
n =โ€”
Only you use (d, n) to decrypt. Never share d.
๐Ÿ“จ
Step 3 โ€” Encrypt with the Public Key
Each character is converted to a number M, then transformed: C = Me mod n. The result is unreadable without the private key.
Formula: C โ‰ก Mแต‰ (mod n)
M is the plaintext number (ASCII code). C is the ciphertext. Anyone with (e, n) can encrypt, but only the holder of d can reverse it.
Each character is encrypted individually using n = ?
๐Ÿ”“
Step 4 โ€” Decrypt with the Private Key
Apply M = Cd mod n to each ciphertext value. Because e and d are mathematical inverses mod ฯ†(n), you recover the original message perfectly.
Formula: M โ‰ก Cแตˆ (mod n)
This works because of Euler's theorem: (Mแต‰)แตˆ โ‰ก M (mod n). The private exponent d "undoes" the encryption.
๐Ÿ›ก๏ธ
Step 5 โ€” Why RSA Is (Computationally) Secure
The gap between "easy to compute" and "hard to reverse" is what makes RSA work.
RSA security rests on the Integer Factorisation Problem: given n = p ร— q, find p and q. For numbers with 2048+ bits, no classical algorithm can do this in any reasonable time.
OperationDifficultyWhy
Multiply p ร— q โ†’ nEasySimple multiplication, O(nยฒ) bit operations
Factor n โ†’ p, qHardBest known: sub-exponential, still ~10ยณโด ops for 2048-bit
Compute e, n โ†’ CEasyFast modular exponentiation (square-and-multiply)
Recover d from (e, n)HardEquivalent to factoring n
Compute d from (e, ฯ†(n))EasyExtended Euclidean Algorithm โ€” O(log n)

๐Ÿ• Time to crack your keys
๐ŸŒ Real-world RSA key sizes
512-bit Broken in hours (1999)
1024-bit Risky, avoid
2048-bit Standard today
4096-bit High security

Quantum threat: Shor's algorithm (1994) can factor n in polynomial time on a quantum computer. NIST standardised CRYSTALS-Kyber (FIPS 203) and CRYSTALS-Dilithium (FIPS 204) in 2024 as quantum-safe replacements.