RU EN ES 中文

RSA: a padlock anyone can snap shut but only one can open

Bob prints the key for letters to him right in the newspaper. How can that key lock a letter but not open it?

Can you hand the key to everyone and keep the secret?

Bob wants anyone to be able to write to him in secret, even people he has never met. He prints a key in the newspaper: “encrypt your letters to me with this”. Eve reads the newspaper too. Can it be that Eve holds the very same key and still can’t read a single letter to Bob?

It sounds like nonsense. In every familiar cipher, from Caesar to AES, the same key both locks and unlocks. Whoever knows how to encrypt also knows how to decrypt: just do everything in reverse. Publishing such a key means publishing the secret itself.

In 1976 Diffie and Hellman showed how two people can agree on a shared key in public. But you have to agree anew with every correspondent, and both have to be online at the same time. In the same paper they described a better dream: a cipher with two different keys — a public one to lock and a private one to unlock. They did not know how to build it.

Key takeaway: A year later three researchers at MIT found the answer. A “one-way” operation already existed in Diffie–Hellman, but nobody at all can undo it. A cipher needs an operation with a trapdoor: anyone can do it one way, but only someone who knows the hint can go back. In RSA the hint is two prime numbers. Anyone who knows their product can lock a letter, and only someone who knows the primes themselves can open it.

A padlock, a key and two primes

RSA is a public-key cipher: Bob has two keys, and they are different. He hands the public key to everyone and shows the private one to nobody. Everyday life has the same idea — a padlock.

Bob hands out open padlocks to everyone and keeps their key. Alice puts a letter in a box and snaps Bob’s padlock shut on it: no key is needed for that. The box travels to Bob by ordinary post. Eve has Bob’s padlock too, but a padlock can only snap shut.

The idea, on padlocks
Anyone can snap it shutEveryone has Bob’s padlocks. Locking the box needs no key.
+=
Only the key opens itThere is one key, and Bob has it. Eve sees the locked box and holds the same padlock, but a padlock can’t open the box.
+=
The key can’t be cut from the padlockEve can study the padlock as long as she likes, but she won’t work out what the key looks like. The whole scheme rests on this.
→?

Simplification: a real padlock can be picked. RSA’s number padlock has a lock pick too — factoring n — but on huge numbers it doesn’t work.

RSA locks with the same operation as Diffie–Hellman: raise a number to a power and take the remainder of a division. What is new is elsewhere: Bob builds the modulus n himself out of two secret primes, and those primes are what let him compute the key. All the examples below use the game’s numbers, where n = 33.

1

Two secret primes p and q

Bob picks two random prime numbers p and q — numbers divisible only by 1 and themselves. In real keys each is 1024 bits long or more, about 309 decimal digits. He hands out their product n = p × q and keeps p and q secret. Multiplying two numbers is easy. Finding which primes a product is made of is hard when the numbers are huge.

Example: p = 3, q = 11, n = 3 × 11 = 33.

2

The padlock’s power e and the key’s power d

Bob computes φ = (p − 1)(q − 1) and picks the padlock’s power e — a number with no common divisors with φ. In real keys it is almost always e = 65537. From e and φ he finds the key’s power d: one for which e · d leaves remainder 1 when divided by φ. The public key is the pair (n, e), the private key is d. Finding d takes φ, and finding φ takes p and q.

Example: φ = 2 × 10 = 20, e = 3, d = 7, because 3 · 7 = 21 = 1 · 20 + 1.

3

Alice locks: c = me mod n

The letter is a number m smaller than n: text is turned into numbers first. Alice takes Bob’s public key and computes c = me mod n. That is “snapping the padlock shut”. The power is worked out in stages: multiply, take the remainder, multiply, take the remainder. The answer is the same as raising to the whole power at once, but the numbers don’t grow. Otherwise no computer could cope with the power 65537 and numbers hundreds of digits long. The number c is sent in the open: without d it is useless.

Example: m = 4. 4 × 4 = 16; 16 × 4 = 64 = 1 · 33 + 31. So c = 31.

4

Bob unlocks: m = cd mod n

Bob raises c to his secret power d with the same modulus and gets the original m. There is no new operation: the method is the same, only the power differs.

Example: 317 mod 33 in stages: 31 · 31 = 961 → 4; 4 · 31 = 124 → 25; 25 · 31 = 775 → 16; 16 · 31 = 496 → 1; 1 · 31 = 31; 31 · 31 = 961 → 4. Bob got m = 4.

5

Why it matches

Alice raised m to the power e, Bob raised the result to the power d. Together that is me·d. And Bob chose e · d to be 1 more than some multiple of φ. Remainders modulo n have a property (Euler’s theorem): such a power brings every number back to where it was. That is why cd = me·d = m mod n. You can picture it like this: the padlock shuffles the numbers from 0 to n − 1 like cards in a deck, and every number has its own place. The key leads back along the same lines.

Example: 3 · 7 = 21 = 1 · 20 + 1, and 421 mod 33 = 4.

So what can Eve do? Eve has n, e and c. To open the letter she needs d. For d she needs φ, for φ she needs p and q, which means Eve has to factor n. 33 = 3 × 11 can be factored in your head. But nobody has factored a 617-digit number: the longest number of this kind ever factored, RSA-250 (2020), is two and a half times shorter and took about 2,700 years of work for a single processor core.
The catch: Eve doesn’t have to factor Everyone has the same padlock, and there is no randomness in it: the same m always gives the same c. If the letter can be guessed — “yes” or “no”, a safe code, a transfer amount — Eve simply locks every option with Bob’s padlock one by one and compares them with the intercepted c. A match means the letter is read, and n was never factored. That is why “textbook” RSA, as in the game, is never used. Before encryption random bytes are mixed into the letter (the OAEP scheme), and the same letter turns into a new ciphertext every time.

A year of searching and a $100 puzzle

Ronald Rivest, Adi Shamir and Leonard Adleman of MIT spent a year looking for the function Diffie and Hellman had dreamed of. Rivest and Shamir proposed schemes, Adleman broke them. A working scheme on two primes appeared in April 1977; the letters RSA are their initials. In August Martin Gardner wrote about the cipher in Scientific American and published a puzzle: a 129-digit number and a phrase encrypted with it, with a $100 prize. It was solved only in 1994: about 600 volunteers spent eight months factoring the number over the internet. The phrase turned out to be “The Magic Words are Squeamish Ossifrage”.

Britain’s GCHQ got there first here too. In 1973 the young mathematician Clifford Cocks read his colleague James Ellis’s note on “non-secret encryption” and in a single evening found the same two-prime scheme — almost four years before Rivest, Shamir and Adleman. MIT’s patent on RSA ran in the US until September 2000; after that the algorithm became free.

RSA still works all around you, though these days it signs more often than it encrypts:

HTTPS

Most websites’ certificates are signed with RSA, and the browser uses that signature to check that the key really belongs to this site. Sending the connection key with RSA, on the other hand, was banned in TLS 1.3: that is now the job of the Diffie–Hellman exchange.

SSH and routers

On the first connection a server or router shows the fingerprint of its public key, and often it is an RSA key. With it the device proves that you are talking to it and not to an impostor.

Signatures and cards

PGP email, signatures on programs and updates, chip bank cards — anywhere the author has to be checked without ever meeting them.

How RSA differs from Diffie–Hellman

Both algorithms belong to public-key cryptography, and both compute powers modulo a number, so they are often confused. The difference is in what they do. In the Diffie–Hellman exchange Alice and Bob swap public numbers and each computes the shared key on their own side. Nothing is encrypted there, and both have to take part at the same time. In RSA Bob already has a padlock handed out to everyone in advance: Alice takes it and locks the letter on her own, even if Bob is offline right now.

QuestionRSADiffie–Hellman
What it is forTo lock a letter for one recipient or to sign your ownFor two people to get a shared key that later encrypts their messages
Must the other side be onlineNo: Alice already has Bob’s padlockYes: each must contribute their own secret number
What is sentThe letter itself, locked with the padlockOnly public numbers; neither the letter nor the key is in the channel
KeysBob’s padlock and key last for yearsThe secret numbers are chosen anew for every conversation
Eve’s hard problemSplit n into two primesFind the secret number from the public one (discrete logarithm)
AnalogyAn open padlockPaints mixed by two people
In one sentence: RSA locks, Diffie–Hellman agrees. That is why modern HTTPS puts them side by side rather than one instead of the other: Diffie–Hellman produces the conversation key, and RSA confirms with a signature that the key came from the site. This closes the “Eve in the middle” hole that Diffie–Hellman can’t close on its own.
RU

The Russian variant: no RSA here

GOST R 34.10-2012 · the national certification authority

Russian standards have no RSA at all. Signatures are made under GOST R 34.10-2012 on elliptic curves, and the shared key is derived with VKO (see the article on Diffie–Hellman). The public key here is a point on a curve, not a product of two primes, so there is nothing to factor. Security rests on a different hard problem — the discrete logarithm.

Locking a letter directly with the public key is not the custom under GOST either: the public key is used only to sign and to derive a shared key, and the letter is then encrypted with a symmetric cipher. In fact, the RSA world does nearly the same: RSA usually locks not the letter itself but a random key for AES.

Yet RSA turns up in Russia every day. Ordinary browsers don’t understand GOST, so even the website certificates issued since 2022 by the national certification authority of the Ministry of Digital Development are signed with RSA. For GOST connections the authority has a separate certificate chain, but such sites open only in browsers that support GOST, such as Yandex Browser.

Where is GOST mandatory? Wherever the law requires certified cryptographic protection tools, above all in government information systems. There everything is signed and encrypted only under GOST, and RSA remains for talking to the outside world.

Try it yourself

In the game you will build the key yourself, lock and open a letter, and then try to break it as Eve. What to do:

What to notice: Breaking by factoring depends on the size of n: on 33 it is instant, on 2048 bits hopeless. Guessing doesn’t care about the size of n at all. The only thing that stops it is the randomness added to every letter.
An unexpected conclusion: RSA’s strength is usually explained by how hard factoring is, yet in the game Eve read the letter without factoring anything. So RSA rests on two things: huge primes and randomness in every letter. Without the second, the simplest attack is enough. And if you swap the keys’ roles — lock with the private key and check with the public one — you get proof that “Bob really wrote this”. How such proof works and where it gets forged is the subject of the “Digital signature” game.
For the curious

RSA and GOST R 34.10-2012 side by side

Both schemes have a key pair, public and private, but the hard problems underneath are different, and so is what they can do:

ParameterRSAGOST R 34.10-2012
What the key is made ofTwo large primes and their productA secret number and a point on an elliptic curve
Hard problemFactoringDiscrete logarithm on a curve
Key size for ≈ 128-bit security3072 bits256 bits
What it can doEncryption (with OAEP) and signaturesSignatures; the shared key via VKO
Year19772012 (before it, GOST R 34.10-2001)
StandardPKCS #1 (RFC 8017)GOST R 34.10-2012 (RFC 7091), TC 26

Factoring records

RSA numbers are factored with the same number field sieve that threatens Diffie–Hellman, so the security is the same too: 2048 bits roughly equal a 112-bit symmetric key, and 128 bits take 3072. How the records grew: RSA-129 (426 bits) was factored in 1994, RSA-768 in 2009, RSA-250 (829 bits) in 2020. 2048 bits is still very far off, but keys shorter than 2048 bits are considered unsafe today.

Why e = 65537

The number 65537 = 216 + 1 is prime, and in binary it has only two ones, so a letter can be locked in 17 multiplications. A very small e, such as 3, is dangerous without added randomness: when m3 is smaller than n, the division changes nothing, and Eve simply takes the cube root of c. The game shows this for m = 2 and 3: 23 = 8 is smaller than 33, and c = 8.

When two keys share a prime

In 2012 two research groups collected millions of RSA public keys from the internet and compared them in pairs. Tens of thousands of keys turned out to share prime factors, mostly on routers, network cameras and other devices that created their key right after first power-on, before they had any good randomness. If two n share a p, Euclid’s algorithm finds it in a fraction of a second, and both keys are broken. Primes must be not only huge but truly random.

What comes next

Shor’s algorithm on a quantum computer would factor n as quickly as it would solve the Diffie–Hellman problem. For RSA the signatures matter most, and their replacement is already prepared: the post-quantum signature standards ML-DSA (FIPS 204) and SLH-DSA (FIPS 205). The second is interesting because it relies only on hash functions, with no number theory at all.

Now you know how to lock a letter with a key everyone has, and why the hardness of factoring alone is not enough

In the game you will choose your own primes, build the padlock’s tape and see Eve read the letter without any factoring