Computer Science · Simulator 057
Build a tiny RSA key pair from two small primes, then encrypt a message with the public key and decrypt it with the private key, one modular calculation at a time. Try to break the key by factoring n, and see why real keys use primes hundreds of digits long.
Class 11–12CollegeBuilds on Class 10 Maths: Real Numbers (primes); Class 11 Computer Science: cyber safety
Key generation:
Encrypt: c = mᵉ mod n. Decrypt: m = cᵈ mod n. This works because of Euler's theorem. Sign with d and anyone can verify with e.
The public key is (n, e). Knowing p and q gives φ and therefore d, so security rests on factoring n being hard. For the toy sizes here it takes microseconds; for 2048-bit keys, no known classical method finishes in any practical time.
Educational only. Tiny keys, no padding, and character-by-character encryption are completely insecure. Never use this for real secrets. Real RSA uses padding schemes (OAEP, PSS) and 2048+ bit keys.
With n = 3233 and e = 17, why does encrypting each letter separately leak information even though the key is “correct”?
The same letter always gives the same ciphertext number, so frequency analysis works exactly as for a simple substitution cipher. Repeated letters are visible in the ciphertext readout. Real RSA adds random padding so equal messages encrypt differently, and it usually encrypts only a random symmetric key.
A tiny key: p = 11, q = 13, so n = 143 and φ = 10 × 12 = 120.
Public exponent: e = 7, which shares no factor with 120. Private exponent: d must satisfy 7d ≡ 1 (mod 120). Since 7 × 103 = 721 = 6 × 120 + 1, d = 103.
Encrypt m = 9: 9² = 81, 9⁴ = 81² mod 143 = 126, so 9⁷ = 126 × 81 × 9 mod 143 = 48. The ciphertext is c = 48.
Decrypt: 48¹⁰³ mod 143 = 9, the original number.
Sign m = 20: s = 20¹⁰³ mod 143 = 58. Anyone can check it with the public key: 58⁷ mod 143 = 20. Set p = 11, q = 13, e = 7 and m = 9 to confirm.
Pick two large primes and multiply them to get n. Encryption raises the message to the public power e mod n; decryption raises the result to the private power d. Only someone who knows the prime factors can work out d.
Finding d needs φ(n), which needs the prime factors of n. Multiplying primes is easy, but nobody knows a fast way to factor a 2048-bit n with ordinary computers.
It is prime and has only two 1 bits in binary, so encryption takes just 17 multiplications. It is also large enough to avoid attacks on very small exponents.