Math

RSA Encryption

RSA multiplies two primes to build a public modulus, then uses modular exponentiation to encrypt and decrypt. Change the toy primes and message to see every step.

8 minLevel 3/3The interactive stage stays ad-free.
n = pq3233φ(n)3120Public e17Private d2753
Choose primesp = 61q = 53
→
Public modulusn = 3233φ(n) = 3120
→
Public key(3233, 17)Private key (3233, 2753)
Message m65“A”
→
c = mᵉ mod n2790ciphertext
→
m = cᵈ mod n65“A”

The public key exposes n and e; the private exponent d is derived from φ(n), which requires factoring n. Encryption raises the message to e, and decryption raises the ciphertext to d. These are toy primes — real RSA uses enormous keys and padding.

What you are seeing

1

Choose primes p and q. The public modulus is n = pq and φ(n) = (p − 1)(q − 1).

2

Pick e coprime to φ(n). The private exponent d satisfies e d ≡ 1 (mod φ(n)).

3

Encrypt with c = mᵉ mod n and decrypt with m = cᵈ mod n. The public sees n and e but not the factorization.

Why it matters

RSA secures websites, software updates, and digital signatures. It is a direct application of primes, modular arithmetic, and the difficulty of factoring large numbers.

Complexity & details

Modular exponentiation is O(log e) multiplications. Factoring n is believed to be hard for large semiprimes, which is what keeps RSA secure.

Frequently asked questions

Why can’t anyone decrypt with the public key?+

The public key gives n and e, but recovering d requires φ(n), which requires factoring n. For large primes, factoring is infeasible.

Is this demo secure?+

No. It uses tiny primes so every step is visible. Real RSA uses keys hundreds or thousands of bits long and secure padding.