Curious math · Cryptography, part 2
A padlock anyone can lock but only one person can open: how does RSA work?
Short answer: RSA uses 2 keys. The public key is like a padlock you hand out to anyone; it is used to lock. The private key stays with you alone; it is used to unlock. Nobody else can work out the private key, because that means splitting a huge number into a product of 2 primes, and no one knows a fast way to do that.
The problem: sending a secret to someone you’ve never met
If one key both locks and unlocks, you first have to get that key to the other person, and it could be stolen on the way. In 1977–1978, Rivest, Shamir and Adleman (RSA for short) proposed a way to publish the locking key without revealing the unlocking key. Anyone who wants to send us a secret can just pick up our padlock and lock it.
Making the keys in 4 steps (small numbers · demo only)
- Pick 2 prime numbers: p = 61, q = 53, and multiply them: n = 61 × 53 = 3233
- Compute φ = (p − 1)(q − 1) = 60 × 52 = 3120
- Choose e = 17 with gcd(e, φ) = 1 ⇒ public key = (n, e) = (3233, 17)
- Find d so that e × d leaves remainder 1 when divided by φ (using the Euclidean algorithm) ⇒ d = 2753, because 17 × 2753 = 46801 = 15 × 3120 + 1 ⇒ private key = d
Euclidean algorithm for 3120 and 17: 3120 = 183 × 17 + 9 · 17 = 1 × 9 + 8 · 9 = 1 × 8 + 1 · 8 = 8 × 1 + 0 ⇒ gcd = 1, and working back up gives d
Lock, then unlock
First turn the message into numbers, e.g. the letter “A” = 65 (its ASCII code)
🔒 Lock: 6517 mod 3233 = 2790 · anyone can do this, because n and e are public
🔑 Unlock: 27902753 mod 3233 = 65 = “A” · you need d, which only the owner has
“mod 3233” = divide by 3233 and keep the remainder · huge powers can be computed quickly by squaring again and again and taking the remainder at every step
Try it
1 · Make your own keys
2 · Lock a message, then unlock it
3 · Be the attacker: split n
You only know the public key. If you can split n into p × q, you can compute d · try dividing by one prime at a time
Why nobody else can unlock it
- To find d you need φ, and to get φ you need p and q. In other words, you must split n into a product of primes
- n = 3233 is tiny: trying just 16 primes finds 53 × 61
- Real keys are 2048 bits or longer ⇒ n is about 617 digits long · NIST rates a 2048-bit RSA key at 112-bit security, and 3072 bits at 128-bit
- ⚠️ Nobody has proved that fast factoring is impossible. The inventors of RSA wrote that they could not prove it either. But so far, nobody knows a method fast enough for numbers that size on ordinary computers
Why unlocking gives back the original message
Because e × d = 15 × φ + 1 ⇒ (me)d = m15φ + 1 = (mφ)15 × m · Euler’s theorem says mφ leaves remainder 1 when divided by n (when m and n share no common factor) ⇒ only m is left · when they do share a factor, you still get m back (we checked every message from 0 to 3,232)
Where it is really used
- Digital signatures: run it backwards, “locking” with the private key so everyone can check with the public key · https websites use signatures like this (RSA, or others such as ECDSA) to prove they are the real site
- ⚠️ TLS 1.3 (the standard behind https) no longer uses RSA to send keys directly · the two sides now agree on a key another way, then use that key to encrypt the data
Watch the video
Watch on YouTube
The video loads from YouTube (youtube-nocookie.com) only after you press play. · YouTube channel: atmath (@atmathapp)
The math behind it
- Directly: prime numbers · gcd and the Euclidean algorithm · remainders (mod) · exponents
- Behind the scenes: Euler’s and Fermat’s theorems (number theory) · factorization
Good to know
- The numbers on this page are very small, and each letter is locked separately (the same letter always gives the same number), so this is a toy for understanding the idea · ⛔ not for real use
- Real RSA adds random padding before locking, following the standard (PKCS #1), instead of the bare formula used on this page
Next (cryptography series, part 3): how a hash works, step by step from letters to a 64-character hash
References
- Rivest, Shamir, Adleman (1978) A Method for Obtaining Digital Signatures and Public-Key Cryptosystems · CACM 21(2)
- NIST SP 800-57 Part 1 Rev. 5 · Recommendation for Key Management (Table 2)
- RFC 9846 · The Transport Layer Security (TLS) Protocol Version 1.3
- RFC 8017 · PKCS #1: RSA Cryptography Specifications v2.2
Accessed 19 Nov 2026 · the numbers on this page were computed with our own script (every message from 0 to n − 1 was locked and unlocked)