✖

RSA cipher: public and private keys, a step-by-step example

The RSA cipher has two keys, a public one and a private one, and thanks to that a complete stranger, with whom you have agreed nothing in advance, can encrypt a message for you. Your browser uses it when it connects to a bank, and electronic documents are signed with it.

Public and private keys

The Caesar cipher and the Vigenère cipher encrypt and decrypt with the same key. Whoever knows it reads everything, so it has to be handed over secretly. RSA works with two. Imagine a postbox with a slot: anyone can drop a letter into it, but only the owner can take it out, because only the owner has the little key. The public key is the slot. The owner can happily put it on a website, and everyone can encrypt a message with it. The private key corresponds to the little key to the postbox: only the owner holds it, and only with it can a message be decrypted.

What mod means

RSA does not work with letters but with numbers, so we first turn the message into a number. It is enough to number the letters according to the alphabet; in a computer every character has its own number. The basic operation is mod, the remainder after division. The notation 17mod5 says: divide 17 by 5 and take the remainder. Five goes into seventeen three times, that is 15, and 2 is left:

17mod5=2

A clock runs on the same principle. At 14:00 the hour hand points to 2, because 14mod12=2. The remainder after division by a number n is always from 0 to n − 1.

An RSA example step by step

Petr wants to receive secret messages. He chooses two prime numbers, that is, numbers divisible only by one and by themselves. Real primes have hundreds of digits, but small ones will do for us: p = 5 and q = 11. He multiplies them:

n=5⋅11=55

The next step is the number phi, φ, which says how many numbers from 1 to 55 have no common divisor with 55 other than one. The 11 multiples of five and the 5 multiples of eleven drop out; only the number 55 is in both, so what remains is 55 − 11 − 5 + 1 = 40. In general we get pq − q − p + 1, which can be written as a product:

φ=(p−1)⋅(q−1)=4⋅10=40

Then he chooses a number e that has no common divisor with 40 other than one, so that their greatest common divisor is 1. For example e = 3 will do. Finally he finds a number d for which 3 · d gives remainder 1 after division by 40. He goes through the numbers with remainder 1, that is 41, 81, 121…, and looks for the first one divisible by three. 41 doesn’t work, but 81 does, so d = 27:

3⋅27=81=2⋅40+1

For large numbers, the extended Euclidean algorithm is used instead of trying, and it finds d quickly.

The pair n = 55 and e = 3 forms the public key and Petr publishes it. He keeps the private key d = 27 to himself, along with p, q and φ:

Number Value Who knows it
p, q 5, 11 only Petr
φ 40 only Petr
n, e 55, 3 everyone (public key)
d 27 only Petr (private key)

Jana wants to send Petr the letter I, the ninth in the English alphabet, that is, the message m = 9. She encrypts it with the public key: she raises it to the power e and takes the remainder after division by n:

c=93mod55=729mod55

55 goes into 729 thirteen times, 13 · 55 = 715, and 14 is left:

c=14

Jana sends 14. Anyone who intercepts it on the way sees only this number and the public key, with which they cannot unlock the message. Petr reaches for the private key and raises the ciphertext to the power d:

1427mod55=9

On the right Petr with a lock n = 55, e = 3 and a key d = 27, the lock travels along a dashed arrow to the left to Jana; Jana calculates from the message m = 9 the number 9³ mod 55 = 14, sends 14 to Petr and he calculates 14²⁷ mod 55 = 9 On the right Petr with a lock n = 55, e = 3 and a key d = 27, the lock travels along a dashed arrow to the left to Jana; Jana calculates from the message m = 9 the number 9³ mod 55 = 14, sends 14 to Petr and he calculates 14²⁷ mod 55 = 9

The number 1427 has 31 digits, but fortunately nobody has to calculate it in full. After every multiplication we take the remainder after division by 55, so the numbers stay small. It is enough to keep doubling the powers:

Power Calculation Remainder after division by 55
142 14 · 14 = 196 31
144 31 · 31 = 961 26
148 26 · 26 = 676 16
1416 16 · 16 = 256 36

From the decomposition 27 = 16 + 8 + 2 + 1 it follows that we only need to multiply 36 · 16 · 31 · 14, again taking the remainder after every step: 576 → 26, 26 · 31 = 806 → 36, 36 · 14 = 504 → 9. The calculator below will do it for you.

Why the message comes back

Jana raised the message to the power 3 and Petr raised the result to the power 27, so altogether m was raised to the power 3 · 27 = 81. And 81 is twice 40 plus one:

m81=m40⋅m40⋅m

In the 18th century, Leonhard Euler proved a theorem according to which mφmodn=1 if m and n have no common divisor. We will leave out the proof, but you can check the theorem yourself on one number: 240 = 1,099,511,627,776, and the division with remainder calculator shows that dividing it by 55 leaves remainder 1. In our example the theorem implies that m40 gives remainder 1 after division by 55 if m is not a multiple of five or eleven. From m81 what remains is 1 · 1 · m, that is, the original message. That is exactly why Petr chose d so that e · d is one more than a multiple of φ. The message comes back even when m is a multiple of five or eleven, but the proof would not fit here.

The keys also work the other way round. When Petr raises a message to his d, anyone can raise the result to the public e and get the message back. But only Petr can produce such a result, and this is what digital signatures are built on.

Why the RSA cipher is secure

The attacker knows n = 55 and e = 3. If he knew φ, he would calculate d just as Petr did. But to get φ he needs p and q, so he has to factorise n into primes. For 55 that is child’s play, 55 = 5 · 11, and the prime factorisation calculator factorises numbers with fifteen digits in a moment.

Real keys, however, have an n that is 2048 bits long. A bit is one digit in the binary system that computers use, and 2048 bits come to 617 ordinary digits. A computer can multiply two such primes at once, but nobody can factorise their product back. The record so far is the number RSA-250 with 250 digits, which a team of mathematicians factorised in February 2020; on a single processor core the calculation would have taken roughly 2,700 years. Today’s keys are more than twice as long. The whole security of RSA thus rests on the fact that multiplying is easy, but factorising is hard.

RSA calculator

Enter two different primes, a public exponent e and a message as a whole number smaller than n. From them the calculator works out both keys, encrypts the message and decrypts the ciphertext again. Try for instance the message 17: the ciphertext comes out as 18. For the primes 11 and 13 with e = 7 the private key comes out as 103.

Who invented RSA

The RSA algorithm bears the initials of its authors: Ronald Rivest, Adi Shamir and Leonard Adleman devised it in 1977 at MIT in the United States. They built on Whitfield Diffie and Martin Hellman, who a year earlier had shown that two people can agree on a secret key entirely in public. But the trio from MIT was beaten to it by the British mathematician Clifford Cocks of the intelligence agency GCHQ, who had come up with the same idea as early as 1973. His discovery, however, stayed secret and was published only in 1997.