Last Updated : 06 Jan, 2025
RSA(Rivest-Shamir-Adleman) Algorithm is an asymmetric or public-key cryptography algorithm which means it works on two different keys: Public Key and Private Key. The Public Key is used for encryption and is known to everyone, while the Private Key is used for decryption and must be kept secret by the receiver. RSA Algorithm is named after Ron Rivest, Adi Shamir and Leonard Adleman, who published the algorithm in 1977.
Example of Asymmetric Cryptography:
If Person A wants to send a message securely to Person B:
RSA Algorithm is based on factorization of large number and modular arithmetic for encrypting and decrypting data. It consists of three main stages:
- Key Generation: Creating Public and Private Keys
- Encryption: Sender encrypts the data using Public Key to get cipher text.
- Decryption: Decrypting the cipher text using Private Key to get the original data.
1. Key Generation
Finally, the Public Key = (n, e) and the Private Key = (n, d).
2. Encryption
To encrypt a message M, it is first converted to numerical representation using ASCII and other encoding schemes. Now, use the public key (n, e) to encrypt the message and get the cipher text using the formula:
C = Me mod n, where C is the Cipher text and e and n are parts of public key.
3. Decryption
To decrypt the cipher text C, use the private key (n, d) and get the original data using the formula:
Example of RSA Algorithm Idea behind RSA AlgorithmM = Cd mod n, where M is the message and d and n are parts of private key.
The idea of RSA is based on the fact that it is difficult to factorize a large integer. The Public Key is (n, e), where n and e are publicly known, while the Private Key is (n, d). Since only the receiver knows the value of d, only they can decrypt the message. But is it possible to find the value of d using n and e?
We know that (d * e) ≡ 1 mod Φ(n), so if we can calculate the value of Φ(n), we can find the value of d. But Φ(n) = (p - 1) * (q - 1). So, we need the value of p and q. Now, one might think that it's quite easy to find the value of p and q as n = p * q and n is already publicly known but RSA Algorithm takes the value of p and q to be very large which in turn makes the value of n extremely large and factorizing such a large value is computationally impossible.
Therefore encryption strength lies in the values of p and q. RSA keys can be typically 1024 or 2048 bits long, but experts believe that 1024-bit keys could be broken shortly. But till now it seems to be an infeasible task.
Note: If someone gets to know the value of p and q, then he can calculate the value of d and decrypt the message.
Implementation of RSA Algorithm C++
// C++ Program for implementation of RSA Algorithm
#include <bits/stdc++.h>
using namespace std;
// Function to compute base^expo mod m
int power(int base, int expo, int m) {
int res = 1;
base = base % m;
while (expo > 0) {
if (expo & 1)
res = (res * 1LL * base) % m;
base = (base * 1LL * base) % m;
expo = expo / 2;
}
return res;
}
// Function to find modular inverse of e modulo phi(n)
// Here we are calculating phi(n) using Hit and Trial Method
// but we can optimize it using Extended Euclidean Algorithm
int modInverse(int e, int phi) {
for (int d = 2; d < phi; d++) {
if ((e * d) % phi == 1)
return d;
}
return -1;
}
// RSA Key Generation
void generateKeys(int &e, int &d, int &n) {
int p = 7919;
int q = 1009;
n = p * q;
int phi = (p - 1) * (q - 1);
// Choose e, where 1 < e < phi(n) and gcd(e, phi(n)) == 1
for (e = 2; e < phi; e++) {
if (__gcd(e, phi) == 1)
break;
}
// Compute d such that e * d ≡ 1 (mod phi(n))
d = modInverse(e, phi);
}
// Encrypt message using public key (e, n)
int encrypt(int m, int e, int n) {
return power(m, e, n);
}
// Decrypt message using private key (d, n)
int decrypt(int c, int d, int n) {
return power(c, d, n);
}
int main() {
int e, d, n;
// Key Generation
generateKeys(e, d, n);
cout << "Public Key (e, n): (" << e << ", " << n << ")\n";
cout << "Private Key (d, n): (" << d << ", " << n << ")\n";
// Message
int M = 123;
cout << "Original Message: " << M << endl;
// Encrypt the message
int C = encrypt(M, e, n);
cout << "Encrypted Message: " << C << endl;
// Decrypt the message
int decrypted = decrypt(C, d, n);
cout << "Decrypted Message: " << decrypted << endl;
return 0;
}
Java
// Java Program for implementation of RSA Algorithm
import java.math.BigInteger;
class GfG {
// Function to compute base^expo mod m using BigInteger
static BigInteger power(BigInteger base, BigInteger expo, BigInteger m) {
return base.modPow(expo, m);
}
// Function to find modular inverse of e modulo phi(n)
static BigInteger modInverse(BigInteger e, BigInteger phi) {
return e.modInverse(phi);
}
// RSA Key Generation
static void generateKeys(BigInteger[] keys) {
BigInteger p = new BigInteger("7919");
BigInteger q = new BigInteger("1009");
BigInteger n = p.multiply(q);
BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
// Choose e, where 1 < e < phi(n) and gcd(e, phi(n)) == 1
BigInteger e = BigInteger.ZERO;
for (e = new BigInteger("2"); e.compareTo(phi) < 0; e = e.add(BigInteger.ONE)) {
if (e.gcd(phi).equals(BigInteger.ONE)) {
break;
}
}
// Compute d such that e * d ≡ 1 (mod phi(n))
BigInteger d = modInverse(e, phi);
keys[0] = e; // Public Key (e)
keys[1] = d; // Private Key (d)
keys[2] = n; // Modulus (n)
}
// Encrypt message using public key (e, n)
static BigInteger encrypt(BigInteger m, BigInteger e, BigInteger n) {
return power(m, e, n);
}
// Decrypt message using private key (d, n)
static BigInteger decrypt(BigInteger c, BigInteger d, BigInteger n) {
return power(c, d, n);
}
public static void main(String[] args) {
BigInteger[] keys = new BigInteger[3]; // e, d, n
// Key Generation
generateKeys(keys);
System.out.println("Public Key (e, n): (" + keys[0] + ", " + keys[2] + ")");
System.out.println("Private Key (d, n): (" + keys[1] + ", " + keys[2] + ")");
// Message
BigInteger M = new BigInteger("123");
System.out.println("Original Message: " + M);
// Encrypt the message
BigInteger C = encrypt(M, keys[0], keys[2]);
System.out.println("Encrypted Message: " + C);
// Decrypt the message
BigInteger decrypted = decrypt(C, keys[1], keys[2]);
System.out.println("Decrypted Message: " + decrypted);
}
}
Python
# Python Program for implementation of RSA Algorithm
def power(base, expo, m):
res = 1
base = base % m
while expo > 0:
if expo & 1:
res = (res * base) % m
base = (base * base) % m
expo = expo // 2
return res
# Function to find modular inverse of e modulo phi(n)
# Here we are calculating phi(n) using Hit and Trial Method
# but we can optimize it using Extended Euclidean Algorithm
def modInverse(e, phi):
for d in range(2, phi):
if (e * d) % phi == 1:
return d
return -1
# RSA Key Generation
def generateKeys():
p = 7919
q = 1009
n = p * q
phi = (p - 1) * (q - 1)
# Choose e, where 1 < e < phi(n) and gcd(e, phi(n)) == 1
e = 0
for e in range(2, phi):
if gcd(e, phi) == 1:
break
# Compute d such that e * d ≡ 1 (mod phi(n))
d = modInverse(e, phi)
return e, d, n
# Function to calculate gcd
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
# Encrypt message using public key (e, n)
def encrypt(m, e, n):
return power(m, e, n)
# Decrypt message using private key (d, n)
def decrypt(c, d, n):
return power(c, d, n)
# Main execution
if __name__ == "__main__":
# Key Generation
e, d, n = generateKeys()
print(f"Public Key (e, n): ({e}, {n})")
print(f"Private Key (d, n): ({d}, {n})")
# Message
M = 123
print(f"Original Message: {M}")
# Encrypt the message
C = encrypt(M, e, n)
print(f"Encrypted Message: {C}")
# Decrypt the message
decrypted = decrypt(C, d, n)
print(f"Decrypted Message: {decrypted}")
JavaScript
// JavaScript Program for implementation of RSA Algorithm
// Function to compute base^expo mod m using BigInt
function power(base, expo, m) {
let res = BigInt(1);
base = BigInt(base) % BigInt(m);
while (expo > 0) {
if (expo & BigInt(1)) {
res = (res * base) % BigInt(m);
}
base = (base * base) % BigInt(m);
expo = Math.floor(Number(expo) / 2);
expo = BigInt(expo);
}
return res;
}
// Function to find modular inverse of e modulo phi(n)
function modInverse(e, phi) {
e = BigInt(e);
phi = BigInt(phi);
for (let d = BigInt(2); d < phi; d++) {
if ((e * d) % phi === BigInt(1)) {
return d;
}
}
return -1;
}
// RSA Key Generation
function generateKeys() {
let p = BigInt(7919);
let q = BigInt(1009);
let n = p * q;
let phi = (p - BigInt(1)) * (q - BigInt(1));
// Choose e, where 1 < e < phi(n) and gcd(e, phi(n)) == 1
let e;
for (e = BigInt(2); e < phi; e++) {
if (gcd(e, phi) === BigInt(1))
break;
}
// Compute d such that e * d ≡ 1 (mod phi(n))
let d = modInverse(e, phi);
return { e, d, n };
}
function gcd(a, b) {
while (b !== BigInt(0)) {
let t = b;
b = a % b;
a = t;
}
return a;
}
// Encrypt message using public key (e, n)
function encrypt(m, e, n) {
return power(m, e, n);
}
// Decrypt message using private key (d, n)
function decrypt(c, d, n) {
return power(c, d, n);
}
// Key Generation
let { e, d, n } = generateKeys();
console.log(`Public Key (e, n): (${e}, ${n})`);
console.log(`Private Key (d, n): (${d}, ${n})`);
// Message
let M = 123;
console.log(`Original Message: ${M}`);
// Encrypt the message
let C = encrypt(M, e, n);
console.log(`Encrypted Message: ${C}`);
// Decrypt the message
let decrypted = decrypt(C, d, n);
console.log(`Decrypted Message: ${decrypted}`);
Public Key (e, n): (5, 7990271) Private Key (d, n): (1596269, 7990271) Original Message: 123 Encrypted Message: 3332110 Decrypted Message: 123Advantages
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4