The Mathematics Behind the RSA Algorithm
RSA is one of the classic public-key cryptographic algorithms. Its mathematics comes primarily from number theory, especially prime numbers, modular arithmetic, Euler's theorem, and modular inverses.
1. The Big Picture
RSA has two keys:
- Public key — anyone can know it.
- Private key — must be kept secret.
Ciphertext → Decryption using private key → Original Message
The fascinating part is that encryption and decryption are based on exponentiation modulo a large number.
Decryption: m = cd mod n
2. First: What Does "mod" Mean?
The word mod means "remainder after division."
Example 1
17 divided by 5 gives:
Therefore:
Another example
because:
Modular arithmetic is extremely important in RSA.
3. Why Prime Numbers?
RSA begins by selecting two large prime numbers:
q = prime number
For learning, we will use small numbers:
q = 53
In real RSA, these numbers are enormously larger.
4. Calculate n
Multiply the two primes:
With our example:
n = 3233
This number n becomes part of both the public
and private keys.
5. Euler's Totient Function
The next mathematical concept is Euler's totient function, written as:
It counts how many positive integers less than or equal to n are relatively prime to n.
A small example
Consider:
Numbers from 1 through 8 that are relatively prime to 8 are:
Therefore:
For RSA
Because p and q are prime:
We can calculate:
For our example:
= (61 - 1)(53 - 1)
= 60 × 52
= 3120
6. Choosing the Public Exponent e
We now choose a number e.
It must satisfy:
and:
In other words, e and ฯ(n) must have no common factor other than 1.
Our example
e = 17
Since:
17 is a valid choice.
Our public key is now:
= (17, 3233)
7. What Does gcd Mean?
gcd means greatest common divisor.
Example
Consider:
Factors of 12:
Factors of 18:
The largest common factor is 6:
But:
So 17 and 3120 are relatively prime.
8. Finding the Private Key d
Now we need a number d satisfying:
With our values:
The solution is:
Let's verify it
Divide 46801 by 3120:
Therefore:
So:
Therefore:
= (2753, 3233)
9. How Do We Find d?
We use the Extended Euclidean Algorithm.
We want:
The Extended Euclidean Algorithm finds integers d and k that satisfy this equation.
In real cryptographic software, this calculation is performed automatically.
10. RSA Encryption
Suppose our message is represented by:
Encryption is:
Substitute our values:
The result is:
So 65 has been transformed into 2790.
↓
Encryption using (17, 3233)
↓
2790
11. RSA Decryption
The recipient has the private key:
Decryption is:
Therefore:
The result is:
The original message has been recovered.
12. Why Does RSA Actually Work?
This is the most important mathematical part.
We choose e and d such that:
This means there is some integer k such that:
Now encryption followed by decryption gives:
Substitute:
Therefore:
Euler's theorem tells us:
Therefore:
Therefore: med ≡ m mod n
That's why decryption gives us the original message.
13. A Tiny RSA Example From Scratch
Let's use extremely small numbers so that we can see the entire process manually.
Step 1: Choose primes
q = 11
Step 2: Calculate n
Step 3: Calculate ฯ(n)
= 2 × 10
= 20
Step 4: Choose e
Choose:
Check:
Good.
Step 5: Find d
We need:
Try d = 7:
21 mod 20 = 1
Therefore:
Keys
| Key | Value |
|---|---|
| Public key | (3, 33) |
| Private key | (7, 33) |
Step 6: Encrypt
Suppose:
Encryption:
= 64 mod 33
= 31
So:
Step 7: Decrypt
The result is:
So:
14. Why Can't an Attacker Simply Calculate d?
This is the central security idea.
The attacker knows:
e
For our example:
e = 17
But to calculate d, we need:
And to calculate ฯ(n), we need:
In other words:
↓
n = p × q
↓
ฯ(n) = (p-1)(q-1)
↓
d
Going forward is easy.
Going backward requires factoring n.
15. A Simple Factoring Example
Suppose an attacker sees:
They could try:
3233 ÷ 3
3233 ÷ 5
...
Eventually they discover:
Then they can calculate:
= 3120
Then they can calculate d.
3233 is trivial to factor. Real RSA uses enormous primes, typically producing RSA moduli such as 2048 bits or larger.
16. The Role of Modular Exponentiation
You may wonder how a computer can calculate something like:
without generating a gigantic number containing thousands of digits.
It uses an efficient technique called modular exponentiation, commonly implemented using repeated squaring.
Example
Suppose we want:
Since:
We calculate powers by repeatedly squaring:
32 mod 7 = 2
34 mod 7 = 4
38 mod 7 = 2
Then:
Therefore:
= 24 mod 7
= 3
So:
17. Chinese Remainder Theorem
Real RSA implementations often use another important number theory concept: the Chinese Remainder Theorem (CRT).
Instead of doing one huge modular exponentiation modulo:
computations can be performed separately modulo p and q and then recombined.
↓
CRT
↓
mod pq
This can make RSA decryption significantly faster.
18. What Mathematics Is Really Doing the Work?
| Mathematical concept | Purpose in RSA |
|---|---|
| Prime numbers | Used to construct n |
| Multiplication | n = p × q |
| Euler's totient | Calculates ฯ(n) |
| GCD | Checks whether e is valid |
| Extended Euclidean Algorithm | Finds d |
| Modular exponentiation | Encryption and decryption |
| Euler's theorem | Explains why decryption reverses encryption |
| Integer factorization | Provides the computational security assumption |
| Chinese Remainder Theorem | Can speed up private-key operations |
19. The Entire RSA Algorithm in One Place
Key Generation
- Choose two large primes p and q.
-
Calculate:
n = pq
-
Calculate:
ฯ(n) = (p-1)(q-1)
-
Choose e such that:
gcd(e, ฯ(n)) = 1
-
Calculate d such that:
ed ≡ 1 mod ฯ(n)
Public Key
Private Key
Encryption
Decryption
RSA turns number theory into a one-way computational problem.