RSA Cryptosystem
What is the RSA?
The RSA algorithm is an asymmetric cryptography algorithm widely regarded as the most secure encryption method. You may wonder what I mean by saying the RSA algorithm is asymmetric. It means it works on two different keys: the Public Key and the Private Key. And, as the name implies, the Public Key is distributed to everyone while the Private Key is kept private. The RSA algorithm is based on how difficult it is to factorize a large integer.
The RSA algorithm is named after those who invented it in 1978: Ron Rivest, Adi Shamir, and Leonard Adleman. (whitepaper)
The following illustration highlights how asymmetric cryptography works:
How does the RSA algorithm work?
RSA algorithm comprises four stages, and those four stages are:
-
Key generation: To generate a private key (to keep) and a public key (to share).
-
Key distribution: Flood the network with the public key.
-
Encryption: The sender encrypts the message using the receiver’s public key.
-
Decryption: The message is decrypted by the receiver using its private key.
1. Key Generation
The key generation process is as follows:
-
Select two prime numbers: Select two large prime numbers, say and .
-
Calculate : Calculate , where
-
Calculate : Calculate Euler's totient function of , where
-
Select : Select an integer such that and is co-prime to .
-
Calculate : Calculate , the modular multiplicative inverse of modulo . Calculate such that can be found using the extended euclidean algorithm.
-
Public key: The public key is .
-
Private key: The private key is .
2. Key Distribution
The public key is distributed, and the private key is kept secret.
3. Encryption
The encryption process is as follows:
-
Convert the plaintext to a number: Convert the plaintext to a number .
-
Encrypt the message: Compute the ciphertext from the plaintext using the public key , where
4. Decryption
The decryption process is as follows:
- Decrypt the message: Compute the plaintext from the ciphertext using the private key , where
Proof
Since is the inverse of modulo , there exists an integer such that . Assuming that , we have that , by Euler's Totient Theorem. Thus, In case , we know that is divisible by either or . It cannot be divisible by both, since . Assume, without loss of generality, that divides . Then, . In this case, By the argument above, . Since, , we conclude that , by the Chinese Reminder Theorem.
Example
-
Choose and .
-
Compute
-
Compute
-
Choose e such that and and are coprime. Let
-
Compute a value for d such that One solution is
-
Public key is
-
Private key is
-
The encryption of is
-
The decryption of is
Pseudocode
p = 3
q = 11
n = x * y
# n = 33.
# compute the totient, phi
int phi = (x-1)*(y-1)
# phi = 20.
int e = findCoprime(phi)
# find an 'e' which is > 1 and is a co-prime of phi.
# e = 7 satisfies the current values.
# Using the extended euclidean algorithm, find 'd' which satisfies
# this equation:
d = (1 mod (phi))/e
# d = 3 for the example values.
public_key = (e=7, n=33)
private_key = (d=3, n=33)
# Given the plaintext P=2, the ciphertext C is :
C = (2^7) % 33 = 29
# To decrypt the cypher text C:
P = (29^3) % 33 = 2
Security of RSA
The security of RSA relies on the assumption that decoding a message without the private key is computationally difficult, though no formal proof guarantees this.
A common attack involves factoring the public modulus into its prime components and . Once is factored, the private key can be computed as the modular inverse of modulo , allowing decryption of any ciphertext .
This approach is impractical because factoring large integers (e.g., digits) is computationally infeasible. RSA's security therefore relies on the hardness of factoring.
Another potential attack is solving the modular root problem, where is recovered directly from without factorization. While inefficient methods exist, no efficient solution has been discovered, making this problem another cornerstone of RSA's security.