Rivest–Shamir–Adleman - RSA Algorithm
We have now covered DES and AES in depth. We have a good foundation in symmetric-key cryptographic algorithms.
But there is a fundamental problem with all the algorithms we have considered so far: How do we share the secret key in the first place?
A dumb idea: Can we encrypt the key too?
Hmm... Not really. To encrypt the key, we need another key. And if we need to share that key securely, we run into the same problem again. We could keep going, but we would never solve the original problem.
If Alice and Bob have never met before, how can they securely establish a shared secret key?
We know that one of the fundamental assumptions in cryptography is that the communication channel between these two parties is untrusted.
Trudy can intercept, modify, or even inject messages into their communication.
This is where a brand-new concept, public-key cryptography, comes into play.
Public-key cryptography uses two keys:
- Public key: A key that can be shared with everyone.
- Private key: A key that must be kept secret by its owner.
Let's consider Alice and Bob.
Alice has her own public and private keys. Bob has his own public and private keys too.
Anyone can encrypt a message using Alice's public key, but only Alice can decrypt it using her corresponding private key.
Of course, Alice and Bob also need a way to authenticate each other's public keys. Otherwise, Trudy could replace a public key with her own and perform a man-in-the-middle attack.
Trapdoor Functions
RSA is built on a simple but powerful mathematical idea.
We need a mathematical operation that is easy to perform in one direction but extremely difficult to reverse without knowing some secret information.
Such a mathematical construct is called a trapdoor one-way function.
Think of it this way: performing the operation is easy, but reversing it is practically impossible unless you know the secret trapdoor.
When I was learning RSA at university, one question came to mind: What if we simply use a mathematical function that does not have an inverse? Would that solve our problem?
Not quite!
What we actually need is a function that is easy to reverse when we have some secret information, but computationally infeasible to reverse without it.
Let's look at a simple mathematical example.
\[ p\times q=n \]
Where \(p\) and \(q\) are two large prime numbers.
- Easy: Given \(p\) and \(q\), calculating \(n\) is extremely easy. Even multiplying two 1000-digit numbers is computationally inexpensive for modern computers.
- Hard: Given \(n\), finding the original prime factors \(p\) and \(q\) is computationally infeasible for sufficiently large, properly generated RSA moduli using currently known practical classical algorithms.
For example, RSA commonly uses a 2048-bit modulus, which is approximately 617 decimal digits long.
That asymmetry is central to the mathematical foundation of RSA.
The Math We Need
Before we build RSA, we need a few mathematical tools.
Modular Arithmetic
This is a simple concept. We get the remainder of a division.
\[ 15\bmod 12=3 \]
\[ 17\bmod 5=2 \]
\[ 25\bmod 7=4 \]
\[ 100\bmod 10=0 \]
Prime Numbers
A prime number is a positive integer greater than 1 that is divisible only by 1 and itself.
Examples are 2, 3, 5, 7, 11, 13, ...
RSA uses very large prime numbers for \(p\) and \(q\).
For a 2048-bit RSA modulus, the two primes are typically around 1024 bits each (approximately 309 decimal digits).
Also, there is another related concept.
Two numbers are coprime if their greatest common divisor (GCD) is 1.
For example, let's take 8 and 15. Neither is a prime number.
But 8 and 15 are coprime because their GCD is 1.
Euler's Totient Function
For a positive integer \(n\), Euler's totient function, denoted by \(\varphi(n)\), counts how many positive integers from 1 to \(n-1\) are coprime with \(n\).
For a prime number \(p\):
\[ \varphi(p)=p-1 \]
Because every number from 1 to \(p-1\) is coprime to a prime.
For the product of two distinct primes \(n=p\times q\):
\[ \varphi(n)=(p-1)(q-1) \]
This is one of the key formulas in RSA.
If we know the factorization of \(n\), computing \(\varphi(n)\) is trivial. For an RSA modulus consisting of two distinct primes, computing its totient also allows us to factor the modulus.
Euler's Theorem
For any integer \(a\) that is coprime with \(n\):
\[ a^{\varphi(n)}\equiv 1\pmod n \]
This leads directly to:
\[ a^{k\varphi(n)+1}\equiv a\pmod n \]
In other words, if we raise \(a\) to a power that is equivalent to 1 modulo \(\varphi(n)\), we get \(a\) back modulo \(n\).
This is the mathematical foundation behind RSA. It explains why the encryption and decryption operations can reverse each other.
Modular Multiplicative Inverse
The number \(d\) is the modular multiplicative inverse of \(e\) modulo \(\varphi(n)\) if:
\[ e\times d\equiv 1\pmod{\varphi(n)} \]
We can also write:
\[ d\equiv e^{-1}\pmod{\varphi(n)} \]
This inverse can be computed using the Extended Euclidean Algorithm.
The inverse exists if and only if \(e\) and \(\varphi(n)\) are coprime:
\[ \gcd(e,\varphi(n))=1 \]
RSA Key Generation
Now we have all the math we need. Let's generate an RSA key pair.
Step 1: Choose Two Large Primes
Pick two large, distinct prime numbers p and q. In practice, each is typically 1024 bits (for RSA-2048) or 2048 bits (for RSA-4096).
For our example, let's use small primes:
p = 61
q = 53
Step 2: Compute n
n = p × q = 61 × 53 = 3233
This n is the modulus it appears in both the public and private keys.
Step 3: Compute phi(n)
phi(n) = (p - 1)(q - 1) = 60 × 52 = 3120
Step 4: Choose the Public Exponent e
Choose e such that:
1 < e < phi(n)GCD(e, phi(n)) = 1(e and phi(n) are coprime)
The most common choice is e = 65537 (which is 2^16 + 1). This value is used in nearly all RSA implementations because:
- It's prime (so it's coprime with most phi(n) values)
- In binary, it's
10000000000000001— only two 1-bits, making modular exponentiation fast
For our small example:
e = 17 (GCD(17, 3120) = 1 ✓)
Step 5: Compute the Private Exponent d
Find d such that:
e × d mod phi(n) = 1
17 × d mod 3120 = 1
Using the Extended Euclidean Algorithm:
d = 2753 (because 17 × 2753 = 46801, and 46801 mod 3120 = 1)
The Key Pair
Public Key: (e, n) = (17, 3233) ← Share with everyone
Private Key: (d, n) = (2753, 3233) ← Keep secret!
The security depends on keeping d secret. And d can only be computed if you know phi(n), which requires knowing p and q. Since factoring n into p and q is hard for large numbers, the private key is safe.
After key generation, p and q should be securely deleted. We only need n, e, and d going forward.
RSA Encryption and Decryption
Encryption
To encrypt a message m (where 0 <= m < n):
c = m^e mod n
That's it. Raise the message to the power e, take the result modulo n. The ciphertext is c.
Decryption
To decrypt ciphertext c:
m = c^d mod n
Raise the ciphertext to the power d, take the result modulo n. We get the original message back.
Why Does This Work?
Decrypt(Encrypt(m)) = (m^e)^d mod n
= m^(e×d) mod n
Since e × d = 1 mod phi(n), we can write e × d = k × phi(n) + 1 for some integer k.
By Euler's theorem:
m^(k × phi(n) + 1) mod n = m^(k × phi(n)) × m mod n
= (m^phi(n))^k × m mod n
= 1^k × m mod n
= m
The math guarantees that encryption followed by decryption gives back the original message.
Worked Example
Using our keys: public (17, 3233), private (2753, 3233).
Encrypt the message m = 65:
c = 65^17 mod 3233
Computing 65^17 mod 3233 using repeated squaring:
65^1 mod 3233 = 65
65^2 mod 3233 = 4225 mod 3233 = 992
65^4 mod 3233 = 992^2 mod 3233 = 984064 mod 3233 = 2149
65^8 mod 3233 = 2149^2 mod 3233 = 4618201 mod 3233 = 2452
65^16 mod 3233 = 2452^2 mod 3233 = 6012304 mod 3233 = 2195
65^17 = 65^16 × 65^1 mod 3233
= 2195 × 65 mod 3233
= 142675 mod 3233
= 2790
Ciphertext: c = 2790
Decrypt:
m = 2790^2753 mod 3233
This is a massive exponentiation, but the repeated squaring algorithm handles it efficiently. The result:
m = 65 ✓
We get our original message back.
RSA Digital Signatures
RSA isn't just for encryption it also enables digital signatures. The operations are reversed:
- Sign:
signature = m^d mod n(use private key) - Verify:
m = signature^e mod n(use public key)
Only the holder of the private key can create a valid signature, but anyone with the public key can verify it. This provides:
- Authentication — The signature proves the message came from the key owner
- Integrity — Any modification to the message invalidates the signature
- Non-repudiation — The signer can't deny they signed it
In practice, you don't sign the message directly you sign a hash of the message:
1. Compute hash: h = SHA-256(message)
2. Sign the hash: signature = h^d mod n
3. Send: message + signature
Verification:
1. Compute hash: h = SHA-256(message)
2. Recover hash from signature: h' = signature^e mod n
3. Compare: h == h'? If yes, the signature is valid
Signing the hash instead of the message is both more efficient (the hash is small, fixed-size) and more secure (it avoids mathematical attacks against raw RSA signatures).
Padding — Why Raw RSA is Dangerous
Everything we've described so far is called textbook RSA or raw RSA. It's mathematically correct but insecure in practice. Here's why:
Problem 1: Deterministic Encryption
Raw RSA is deterministic — the same plaintext always produces the same ciphertext. An attacker who knows you're encrypting one of a small set of messages (say, "yes" or "no") can encrypt both, compare with the ciphertext, and determine the message.
Problem 2: Malleability
If an attacker has ciphertext c = m^e mod n, they can compute:
c' = c × 2^e mod n = (m × 2)^e mod n
This produces a valid ciphertext for the message 2m without knowing m or d. The attacker can manipulate ciphertexts without decrypting them.
Problem 3: Small Message Attacks
If the message m is small and e = 3, then m^3 might be less than n. In that case, the attacker can just compute the regular (non-modular) cube root of the ciphertext to recover m.
The Solution: Padding Schemes
Modern RSA always uses a padding scheme that adds randomness and structure to the message before encryption:
OAEP (Optimal Asymmetric Encryption Padding) for encryption. Adds randomness and a hash-based mask. Defined in PKCS#1 v2.
Padded message = OAEP_Encode(message, random_seed)
Ciphertext = (Padded message)^e mod n
PSS (Probabilistic Signature Scheme) For signatures. Adds randomness to make signatures non-deterministic.
PKCS#1 v1.5 — An older padding scheme still widely used. Simpler than OAEP but vulnerable to Bleichenbacher's attack (a padding oracle attack discovered in 1998). Avoid for new implementations.
The rule: never use raw RSA. Always use OAEP for encryption and PSS for signatures.
Key Sizes and Security
RSA's security depends on the difficulty of factoring n. As factoring algorithms improve and computers get faster, larger keys are needed.
| RSA Key Size | Security Level (bits) | Status |
|---|---|---|
| 512 bits | ~30 bits | Broken in 1999 |
| 768 bits | ~60 bits | Broken in 2009 (took 2 years) |
| 1024 bits | ~80 bits | Deprecated — factorable with resources |
| 2048 bits | ~112 bits | Current minimum standard |
| 3072 bits | ~128 bits | Recommended for new systems |
| 4096 bits | ~140 bits | High security |
Notice the asymmetry: a 2048-bit RSA key provides only ~112 bits of security, while a 128-bit AES key provides 128 bits. RSA keys need to be much larger than symmetric keys for equivalent security. This is because factoring is easier than brute-force key search.
Comparison with AES
| Symmetric (AES) | Asymmetric (RSA) | Security Level |
|---|---|---|
| 80 bits | 1024 bits | Legacy (broken) |
| 112 bits | 2048 bits | Current minimum |
| 128 bits | 3072 bits | Strong |
| 192 bits | 7680 bits | Very strong |
| 256 bits | 15360 bits | Maximum |
This is why we don't encrypt large data with RSA — the keys and operations are enormous compared to AES. Instead, RSA is used to encrypt a symmetric key, and AES encrypts the actual data. This is called hybrid encryption.
Hybrid Encryption
In practice, nobody encrypts data directly with RSA. It's too slow and has size limitations (the message must be smaller than n). Instead:
Sender:
1. Generate a random AES key (e.g., 256 bits)
2. Encrypt the data with AES: ciphertext = AES_Encrypt(data, aes_key)
3. Encrypt the AES key with RSA: encrypted_key = aes_key^e mod n
4. Send: encrypted_key + ciphertext
Receiver:
1. Decrypt the AES key with RSA: aes_key = encrypted_key^d mod n
2. Decrypt the data with AES: data = AES_Decrypt(ciphertext, aes_key)
This gives you the best of both worlds:
- RSA's key distribution advantage (no need to pre-share a secret key)
- AES's performance (fast bulk encryption)
This is exactly how TLS (HTTPS) worked before TLS 1.3 — the RSA key exchange encrypted a pre-master secret, which was used to derive AES session keys.