RSA is a way to lock a message with a number that everyone can know and unlock it with a number that only you hold. It is named after Rivest, Shamir and Adleman, who published it in 1977, and it solves the problem of getting a secret to someone you have never met. This page builds a complete toy RSA key with numbers small enough to check by hand, then explains what changes in the real thing.
Two ideas RSA is built from
The first idea is the remainder, written mod. The expression x mod n means the remainder when x is divided by n, which is where you land after counting x steps around a clock with n positions. So 17 mod 5 is 2, and on a 12-hour clock 25 mod 12 is 1. Powers work the same way: 34 mod 7 means 81 mod 7, which is 4. The answer never grows past n, and the remainder alone does not reveal how many laps of the clock were made.
The second idea is the prime number, a number divisible only by 1 and itself, such as 53 or 61. Multiplying two primes takes one step. Finding them again from their product is slow, and for large primes it is hopelessly slow. The whole security of RSA rests on that gap.
Building a toy key
Suppose the bank wants to receive secrets. It builds its key in three steps, shown here with the numbers from the interactive story.
step 1: the public number n
The bank picks two secret primes, p = 61 and q = 53, and publishes only their product, n = 61 × 53 = 3233.
step 2: phi(n) and the public exponent e
The private key will be built from a number called phi(n), read “phi of n”, which counts the whole numbers from 1 to n − 1 that share no factor with n apart from 1. For n = 15 those numbers are 1, 2, 4, 7, 8, 11, 13 and 14, so phi(15) = 8. When n is the product of two primes there is a shortcut: phi(n) = (p − 1) × (q − 1), which gives 2 × 4 = 8 for 15 = 3 × 5. For the bank, phi(3233) = 60 × 52 = 3120. It then picks e = 17, which shares no factor with 3120.
step 3: the private key d
The private key is the number d for which e × d leaves remainder 1 when divided by phi(n). Here d = 2753, because 17 × 2753 = 46,801 = 15 × 3120 + 1. Finding d requires phi(n), and phi(n) requires p and q.
| Part | Value | Who knows it |
|---|---|---|
| Public key | n = 3233 and e = 17 | Everyone |
| Private key | d = 2753 | Only the bank |
| Secrets behind it | p = 61, q = 53, phi(n) = 3120 | Only the bank |
Encrypting and decrypting the number 65
Any message can be written as a number, so take the number 65. Anyone can lock it with the public key by raising it to the power e and keeping the remainder:
6517 mod 3233 = 2790
The bank unlocks it with the private key:
27902753 mod 3233 = 65
The two powers cancel because e × d is one more than a multiple of phi(n), and a result called Euler's theorem guarantees that raising to such a combined power returns the original number. A computer keeps only the remainder after every multiplication, so numbers as large as 27902753 never have to be written out.
Why n must be two primes
Anyone who knows p and q can compute phi(n) instantly and then d. If n were a single prime, phi(n) would simply be n − 1, so everyone could work out d and the private key would be public. With two primes, Mallory holds n, e and the locked number. Guessing d is hopeless because a real d is about as large as n. Her only route is to split n back into p and q, which takes 52 trial divisions for 3233 and roughly 10308 for a real 617-digit n.
The best known factoring method, the number field sieve, is far faster than trial division, but its cost still grows steeply. The largest RSA challenge number factored in public, RSA-250 with 829 bits, took about 2,700 core-years in 2020, and real keys are 2048 bits or more.
What real RSA adds
Real keys use primes of about 300 digits each, which the bank can find in a fraction of a second with fast probabilistic tests. Real systems also add random padding, a scheme called OAEP, so that locking the same message twice gives different results. Without padding, the toy version always turns 65 into 2790, so an attacker could test guesses.
Why RSA is only used briefly
RSA is thousands of times slower than AES, and one operation with a 2048-bit key can lock only about 190 bytes. So secure systems use it for a moment at the start and then let a fast symmetric cipher carry the data. Early versions of TLS did exactly this: the browser locked a fresh session key with the bank's RSA public key and sent it. The flaw was that one long-lived private key protected every recording, which is why TLS 1.3 dropped RSA key transport in favour of Diffie-Hellman.
RSA signatures
RSA works in reverse too. To sign, the owner computes a hash, which is a short fixed-size fingerprint of the message, and raises it to the private power d. Anyone can raise the signature to the public power e and compare the result with the hash of the message they received. A match shows the message came from the holder of d and has not changed.
sign and check by hand
Take n = 33, e = 3 and d = 7, and let the message's fingerprint be 5.
- Sign:
57 mod 33 = 14, so the signature is 14. - Check:
143 mod 33 = 5, which equals the fingerprint, so the signature is valid.
If Mallory changes the message, the fingerprint changes and the check fails, and she cannot make a new signature without d. In TLS the server signs the handshake this way, and certificates are signatures on public keys.
Is RSA still safe?
A properly sized RSA key is not broken on any ordinary computer, and there is no public evidence that anything else breaks it today. The long-term concern is a large quantum computer, because Shor's algorithm factors numbers efficiently. No such machine exists yet, and the post-quantum page covers what is being done in advance.