-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.cpp
146 lines (137 loc) · 2.67 KB
/
RSA.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
using namespace std;
int power(int a, int b, int mod)
{
int x = a;
int result = 1;
int count = 0;
cout << "--------------------------------------------------" << endl;
while (b > 0)
{
if ((b & 1) == 1)
result = (result * a) % mod;
if ((b & 1) == 1)
cout << "Binary Expo: " << x << "^" << (1 << count) << endl;
a = (a * a) % mod;
b >>= 1;
count++;
}
cout << "--------------------------------------------------" << endl;
return result;
}
int encryption(int e, int m, int n)
{
if (m < n)
{
int cipher = power(m, e, n);
return cipher;
}
else
{
return -1;
}
}
int gcd(int a, int b)
{
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int decryption(int d, int c, int n)
{
int message = power(c, d, n);
return message;
}
int extendedEuclidean(int r1, int r2)
{
int t1 = 0;
int t2 = 1;
while (r2 > 0)
{
int q = r1 / r2;
int rem = r1 % r2;
int t = t1 - (q * t2);
r1 = r2;
r2 = rem;
t1 = t2;
t2 = t;
}
return t1;
}
int calculateD(int e, int phi)
{
int d = extendedEuclidean(phi, e);
if (d < 0)
{
d += phi;
}
return d;
}
int calculateE(int phi)
{
for (int i = 2; i < phi; i++)
{
if (gcd(phi, i) == 1)
{
return i;
}
}
return -1;
}
bool isPrime(int num)
{
if (num <= 1)
{
return false;
}
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
int main()
{
cout << "Enter the value of p: ";
int p;
cin >> p;
cout << "Enter the value of q: ";
int q;
cin >> q;
if (isPrime(p) && isPrime(q))
{
int n = p * q;
int phi = (p - 1) * (q - 1);
int e = calculateE(phi);
int d = calculateD(e, phi);
cout << "phi(n): " << phi << endl;
cout << "e: " << e << endl;
cout << "d: " << d << endl;
int message;
cout << "Enter the Message: ";
cin >> message;
int cipher = encryption(e, message, n);
if (cipher != -1)
{
cout << "Encrypted Text: " << cipher << endl;
int decryptedMessage = decryption(d, cipher, n);
cout << "Decrypted Text: " << decryptedMessage << endl;
}
else
{
cout << "Encryption not possible, choose big p and q." << endl;
}
}
else
{
cout << "Both p and q should be prime." << endl;
}
return 0;
}