-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa_21BCE3982.cpp
47 lines (37 loc) · 1.2 KB
/
rsa_21BCE3982.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cmath>
using namespace std;
// Function to calculate the modular exponentiation
int modPow(int base, int exponent, int modulus)
{
int result = 1;
base = base % modulus;
while (exponent > 0)
{
if (exponent % 2 == 1)
result = (result * base) % modulus;
exponent = exponent >> 1;
base = (base * base) % modulus;
}
return result;
}
int main()
{
int p = 61; // Prime number p
int q = 53; // Prime number q
int modulus = p * q; // Modulus
int phi = (p - 1) * (q - 1); // Euler's Totient Function
int publicKey = 17; // Public key (e)
int privateKey = 2753; // Private key (d)
int message = 389; // Message to be encrypted
// Encryption
int encryptedMessage = modPow(message, publicKey, modulus);
// Decryption
int decryptedMessage = modPow(encryptedMessage, privateKey, modulus);
cout << "Original Message: " << message << endl;
cout << "Public Key : " << publicKey << endl;
cout << "Private Key : " << privateKey << endl;
cout << "Encrypted Message: " << encryptedMessage << endl;
cout << "Decrypted Message: " << decryptedMessage << endl;
return 0;
}