-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRsa.cpp
More file actions
324 lines (254 loc) · 6.35 KB
/
Rsa.cpp
File metadata and controls
324 lines (254 loc) · 6.35 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "Rsa.hpp"
#include "Cipher.hpp"
#include <cmath>
Rsa::Rsa()
{
Select_Encrypt();
} //end of Rsa::Rsa
void Rsa::GetPrimes()
{
ifstream infile;
string prime;
vector <long long int> prime_array;
cpp_int conv_prime;
// int rand_index1;
// int rand_index2;
cpp_int long_prime;
// infile.open("primes.txt");
// if(infile.fail())
// {
// cout<<"ERROR...";
// exit(EXIT_FAILURE);
// }
// while (infile.get(long_prime))
// { srand(time(0));
//
// conv_prime=long_prime; //opening file getting list of ormes
// prime_array.push_back(static_cast<unsigned long long int>(conv_prime)); //assigning primes to vector array
// }
// srand(time(NULL));
p = 1000000321;
q = 1000000289;
// do
// {
// rand_index1 = rand() % 5;
// p = prime_array[rand_index1]; //found first prime
//
// rand_index2 = rand() % 5 ;
// q = prime_array[rand_index2];
//
// } while (p == q or p == 0 or q == 0);
// cout << p << endl << q << endl;
// for(int i = 0; i <static_cast<int>( prime_array.size()); i++)
// {
// cout << prime_array[i] << endl;
//
// }
} // end of Rsa::GetPrimes
void Rsa::GetE()
{
srand(time(0));
do{
do{
e = rand() % 20;
t = extended_gcd( e , phi_n); // e and phi_n should be relatively prime
}while(get<0>(t) != 1 or e == 1);
cpp_int long_e = e;
cpp_int long_phi_n = phi_n;
d = boost::integer::mod_inverse(long_e, long_phi_n);
}while(d == 0);
// d = 17820319500;
// while (1) {
// if ((d * e) % phi_n == 1)
// break;
// d++;
// }
} //Rsa::GetE()
void Rsa::Select_Encrypt()
{
//User choosing Encrypt or Decrypt or menu
int userNum;
system("clear");
cout << "0. Encrypt with rsa" << endl << endl
<< "1. Decrypt with rsa" << endl << endl
<< "2. Go back to menu" << endl;
do{
cout << "Enter a number: ";
cin >> userNum;
} while ((userNum != 0) and (userNum != 1) and (userNum != 2)); //choosing between 0, 1, 2
//Encrypting with rsa
if(userNum == 0)
{
Encrypt_Rsa();
}
//Decrypting with Rsa
else if(userNum == 1)
{
Decrypt_Rsa();
}
//Go back to menu
else
{
Cipher c1;
c1.Start();
}
} //end of "Select_Encrypt"
void Rsa::Encrypt_Rsa()
{
cpp_int message;
cpp_int modMessage;
GetPrimes();
n = p*q;
phi_n = (p -1) * (q -1);
GetE();
//prompt user to input the message
cout << "Enter a message you want to Encrypt(max 9letters): ";
cin.ignore();
getline(cin, original);
message = strToInt(original);
cout << "original int: " << message << endl;
modMessage = message % n;
result = modMessage;
cpp_int result2 = exponentMod(modMessage, e, n);
// for(int i = 1; i < e; ++i)
// {
// result = ((result * modMessage) % n) ;
// if(result < 0){
// cout << result << " result overflow" << endl;
// }
// }
//result = static_cast<unsigned long long int>(pow(modMessage, e)) % n;
//display Encrypted message
cout << "\n\nResult for Encrypting " << original << " with Rsa:\n\n"
// << "Message: " << result << endl
<< "Message: " << result2 << endl
<< "public key e: " << e << endl
<< "public key n: " << n << endl
<< "private key d: " << d << endl;
if(again())
{
Encrypt_Rsa();
}
}
//end of Rsa::Encrypt_Rsa
void Rsa::Decrypt_Rsa()
{
string encrypted_message;
string message;
//prompt user to input the message
cout << "Enter a message you want to decrypt: ";
cin >> encrypted_message;
cout << "Enter the public key e: ";
cin >> e;
cout << "Enter the public key n: ";
cin >> n;
cout << "Enter the private key d: ";
cin >> d;
cpp_int result(encrypted_message);
cpp_int int_encrypted_message(encrypted_message);
result = exponentMod(int_encrypted_message, d, n);
// for(long long int i = 1; i < d; ++i)
// {
// result = (result * int_encrypted_message) % n;
// if(i % 10000000 == 0)
// {
// cout << result << " i:" << i << " d:" <<d << endl;
// }
// }
message = intToStr(result);
//result = static_cast<unsigned long long int>(pow(modMessage, e)) % n;
//display Encrypted message
cout << "\n\nResult for decrypting " << encrypted_message << " with Rsa:\n\n "<< endl << endl
<< "decrypted int: " << result << endl
<< "Message: " << message << endl
<< "public key e: " << e << endl
<< "public key n: " << n << endl
<< "private key d: " << d << endl;
if(again())
{
Decrypt_Rsa();
}
} //end of Rsa::decrypt_Rsa
cpp_int Rsa::exponentMod(cpp_int A, cpp_int B, cpp_int C)
{
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
cpp_int y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (cpp_int)((y + C) % C);
}
// Recursive function to demonstrate the extended Euclidean algorithm.
// It returns multiple values using tuple in C++.
tuple<int, int, int> Rsa::extended_gcd(int a, int b)
{
if (a == 0) {
return make_tuple(b, 0, 1);
}
int gcd, x, y;
// unpack tuple returned by function into variables
tie(gcd, x, y) = extended_gcd(b % a, a);
return make_tuple(gcd, (y - (b/a) * x), x);
} //returniing gcd, x, y
long long int Rsa::strToInt(string str)
{
long long int result = 0;
int alphaVal;
for(long unsigned int i = 0; i < str.length(); i++)
{
if (str[i] == ' ')
{
alphaVal = 30;
}
else
{
alphaVal = tolower(str[i]) - 'a' +1;
}
result += alphaVal * static_cast<long long int>( pow(10, 2*i));
}
return result;
}
string Rsa::intToStr(boost::multiprecision::cpp_int text)
{
string result;
int numChar = 0;
boost::multiprecision::cpp_int temp = text;
int alphaVal;
while(temp)
{
temp /= 100;
numChar++;
}
for(int i = 0; i < numChar; i++)
{
alphaVal = static_cast<unsigned long long int>(text) % 100;
if (alphaVal == 30)
{
result.push_back(' ');
}
else
{
result.push_back('a' + alphaVal -1);
}
text /= 100;
}
return result;
}
bool Rsa::again()
{
char response;
cout << "\nDo you want to do it again?(y/n): ";
cin >> response;
return (tolower(response) == 'y');
}