Why Does RSA Actually Work?
March 23, 2023 by patrickd
RSA encryption appears so incredibly elegant and simple, it's as if it were some sort of mathematical magic. This article is an attempt to give the reader some intuition about why it works and make the seemingly magical aspects graspable. Note that it avoids mathematical proves where possible, it does not attempt to give an explanation on how to implement RSA securely or efficiently, and it skips any concepts that have been deemed irrelevant for understanding the "why".
The Magic
and chosen as large random prime numbers
, phi being "Euler's Totient Function"
, fullfills requirements of and , combined with makes up the public key
, combined with makes up the private key
, is the encrypted ciphertext of the message
, is the decrypted message from the ciphertext
What is ?
You might already know that the triple line equal sign stands for "congruence" rather than "equality" while is short for modulo which yields the remainder of a division operation.
In congruence both sides have the same remainder when divided by a particular modulus.
Example
and
therefore
and
What is ?
One might expect this to be , but we're only dealing with integers here and not rationals, so what could it be?
According to RSA the following should be true:
The public exponent and the private exponent are cancelling each other out, making them "inverse" to each other. So in modular arithmetic is the "modular multiplicative inverse" of .
Knowing that, we can also see why the notiation of was chosen to represent the inverse when we go back to using rational numbers:
Why does inversion work?
At first it looks quite simple: We just have to find two integers and such that when multiplied with each other they result in 1 (within ) and are therefore inverse to each other:
An interesting aspect to this is, that it only has solutions when and are "coprime" (or "relatively prime") to . Meaning that the greatest common divisior, the biggest integer that divides both numbers without a remainder, is 1.
and
Example
Let's look for the inverse of 5 within :
With there should not be a solution for , is that true?
| d | 0 | 1 | 2 | 3 | 4 | 5 | 6 | ... |
|----------------|----|----|----|----|----|----|----|----------|
| ((d⋅5) mod 15) | 0 | 5 | 10 | 0 | 5 | 10 | 0 | {0,5,10} |
The numbers this can yield quickly start to repeat and none of them are 1, so there is indeed no inverse.
Okay, but 15 is a multiple of 5 so the result is not that much of a surprise.
What about 6?
With there should still not be a solution for , but this time 15 is clearly not a multiple of 6 so what happens now?
| d | 0 | 1 | 2 | 3 | 4 | 5 | 6 | ... |
|----------------|----|----|----|----|----|----|----|--------------|
| ((d⋅6) mod 15) | 0 | 6 | 12 | 3 | 9 | 0 | 6 | {0,3,6,9,12} |
Still no inverse, but something interesting becomes visible: The numbers yielded are always multiples of .
Let's try a number which should actually have an inverse: . It may be worth pointing out here that neither numbers are prime, but with this result they are still "relative prime" to each other.
| d | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | ... |
|----------------|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|--------------------------------------|
| ((d⋅8) mod 15) | 0 | 8 | 1 | 9 | 2 | 10 | 3 | 11 | 4 | 12 | 5 | 13 | 6 | 14 | 7 | 0 | 8 | 1 | {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14} |
Not only has 8 an inverse but the numbers yielded are also all numbers from 0 to 14. Thanks to the greatest common divisor being 1, the resulting numbers are multiples of 1 as well. This time it is as if there's a 1-to-1 mapping between and the resulting numbers as long as .
Why does an inverse from still seem to work in ?
We've seen that the inverse of an integer depends on the modulo. The inverse for 8 in is 2. But in it would be 5.
In RSA is calculated as the inverse of in :
and therefore
But during decryption and appear to be cancelling each other out even under , why does that work?
Unfortunately there doesn't seem to be a good way to explain this without making use of (a little bit of magic called) Euler's Totient Theorem which shows that for an integer that is coprime with the following applies:
To make use of that we have to look for a in the message's exponent and we can quite easily find it:
with the integer
Thanks to Euler's Theorem it seems like we can remove everything except for message from the formula with .
But you might remember I mentioned the Theorem should only apply when is coprime with , which would restrict the valid messages we would be able to encrypt. If was a prime that would not be an issue as all integers would be relatively prime to it.
Why does Euler's Theorem still seem to apply for ?
First, note that because both and are prime their greatest common divisor is always 1. If is not coprime with that must mean that it is a multiple of either or , meaning it can't be coprime with one of them. But it must be coprime with at least one of them, because in order to not be coprime with both of them it must be a multiple of both and and that cannot be the case as long as since is the first possible number to be a multiple of both: .
Therefore even when either or will share a residue of 0 with the message (since would be a multiple of it without leaving a remainder) and the other one would be either:
or
With these, we have either one of the following systems of congruence:
,
or
,
Both of which can be simplified using the Chinese Remainder Theorem to:
Example
with
with
Despite not being coprime with thanks to:
therefore
therefore
Therefore
Why does Euler's Theorem work?
First of all, Euler's Totient Theorem is a generalization of Fermat's Little Theorem where for a prime for any integer the following is true:
Next, we need to look at Euler's function, denoted as . This function counts the number of positive integers up to that are relatively prime to . In other words, it counts the number of integers that have a greatest common divisor of 1 with . When is a prime number , all the numbers from to are relatively prime to , so .
Euler's Theorem generalizes Fermat's Little Theorem and states that for any positive integer and any integer relatively prime to , we have:
Which is in case for prime equivalent to:
Example
A very simple way to gain some intution for these Theorems is through, what one might call, a modulo-lookup-table:
| 0 | 1 | 2 | 3 | 4 |
|------|------|------|------|------|
| 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 |
| 30 | 31 | 32 | 33 | 34 |
: : : : : :
| 80 | 81 | 82 | 83 | 84 |
: : : : : :
| 240 | 241 | 242 | 243 | 244 |
| 245 | 246 | 247 | 248 | 249 |
| 250 | 251 | 252 | 253 | 254 |
| 255 | 256 | 257 | 258 | 259 |
: : : : : :
| 1020 | 1021 | 1022 | 1023 | 1024 |
: : : : : :
Imagine this (modulo 5) lookup-table just continues to infinity and always tells us at what remainder we would end up with if we applied .
Now let's check :
0**5 mod 5 = 0 mod 5
1**5 mod 5 = 1 mod 5
2**5 mod 5 = 32 mod 5
3**5 mod 5 = 243 mod 5
4**5 mod 5 = 1024 mod 5
With the help of the lookup-table we can easily determine the remainder of the division by checking in which column we find the number. The Theorem works.
Although it's mathematically equivalent, let's check too:
0**5 mod 5 = ( 0 - 0) mod 5 = 0 mod 5
1**5 mod 5 = ( 1 - 1) mod 5 = 0 mod 5
2**5 mod 5 = ( 32 - 2) mod 5 = 30 mod 5
3**5 mod 5 = ( 243 - 3) mod 5 = 240 mod 5
4**5 mod 5 = (1024 - 4) mod 5 = 1020 mod 5
Here we can see that we always end up in the 0-column, just as we should.
Finally, let's look at :
0**4 mod 5 = 0 mod 5
1**4 mod 5 = 1 mod 5
2**4 mod 5 = 16 mod 5
3**4 mod 5 = 81 mod 5
4**4 mod 5 = 256 mod 5
Since 0 can't have a greatest common divisor with anything, it also can't be coprime so it yields 0 instead of 1. But as long as the base is coprime to the modulo we always end up in the 1-column.
What is the Chinese Remainder Theorem?
The last trick to explain is why the Chinese Remainder Theorem shows that despite not being coprime to both and .
The Chinese Remainder Theorem (CRT) allows us to solve a system of linear congruences with pairwise coprime moduli. It states that if you have a system of linear congruences like:
x ≡ a1 (mod n1)
x ≡ a2 (mod n2)
...
x ≡ ak (mod nk)
where are pairwise coprime (i.e., they share no common factors other than 1), then there exists a unique solution for modulo the product of the moduli, i.e., modulo ... :
...
In the context of RSA, we have where and are distinct prime numbers, and thus, they are coprime.
m ≡ m (mod p)
m ≡ m (mod q)
Example
Based on the systems of congruence from the previous RSA calculation example:
We now use the CRT step by step: