-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.cpp
230 lines (169 loc) · 5.23 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
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
#include "include/RSA.h"
#include "include/IO.h"
#include "include/Algorithm.h"
#include "include/Random.h"
#include "include/Converter.h"
using std::to_string;
using std::fstream;
RSA::RSA(uint32_t byteCount, int base)
{
this->byteCount = byteCount;
this->base = base;
//? Random ra hai số nguyên tố p và q
generatePrimes();
//? Tính n và phi
calculateNandPhi();
//? Sinh khóa e nguyên tố cùng nhau với phi
generateEncryptionKey();
//? Sinh khóa d là nghịch đảo modulo của e
generateDecryptionKey();
}
void RSA::generatePrimes()
{
if (byteCount == 0) return;
io.writeConsole("Generating p and q...");
do {
p = random.next(byteCount);
//io.writeLog("[RSA::generatePrimes] random p: ", p, base);
} while (p.isPrime(RSA::checkPrimeLoops) == false);
io.writeOutput("[RSA::generatePrimes] prime p: ", p, base);
do {
q = random.next(byteCount);
//io.writeLog("[RSA::generatePrimes] random q: ", p, base);
} while (q.isPrime(RSA::checkPrimeLoops) == false);
io.writeOutput("[RSA::generatePrimes] prime q: ", q, base);
io.writeConsole("Finish generating p and q!");
}
void RSA::calculateNandPhi()
{
if (p == 0 || q == 0) return;
io.writeConsole("Calculating n and phi...");
n = p * q;
phi = (p - 1) * (q - 1);
io.writeOutput("[RSA::RSA] n: ", n, base);
io.writeOutput("[RSA::RSA] phi: ", phi, base);
io.writeConsole("Finish calculating n and phi!");
}
void RSA::generateEncryptionKey()
{
if (phi == 0) return;
io.writeConsole("Generating encryption key e...");
do {
e = random.next(byteCount);
if (e.isEven()) continue; //? e cần phải lẻ
} while (gcd(e, phi) != 1);
io.writeConsole("Finish generating encryption key e!");
io.writeOutput("[RSA::generateEncryptionKey] e: ", e, base);
}
void RSA::generateDecryptionKey()
{
if (phi == 0 || e == 0) return;
io.writeConsole("Generating decryption key d...");
d = inverseMod(e, phi);
io.writeConsole("Finish generating decryption key d!");
io.writeOutput("[RSA::generateDecryptionKey] d: ", d, base);
}
string RSA::encrypt(string plainText) {
string cipherText = "";
if (e == 0 || n == 0)
{
io.writeConsole("Keys is missing!!!");
return cipherText;
}
io.writeLog("[RSA::encrypt] encrypting " + plainText);
for (char character : plainText)
{
io.writeLog("[RSA::encrypt] encrypt: " + to_string(character));
BigInt m = (int)character;
//io.writeLog("[RSA::encrypt] ascii value: ", m, base);
BigInt c = powMod(m, e, n);
//io.writeLog("[RSA::encrypt] cipher text: ", c, base);
if (base == BigIntBase::BASE_10)
cipherText += (converter.bigIntToDecimalStr(c) + " ");
else if (base == BigIntBase::BASE_2)
cipherText += (converter.bigIntToBinaryStr(c) + " ");
}
io.writeLog("[RSA::encrypt] finish encrypting!");
return cipherText;
}
string RSA::decrypt(string cipherText) {
string plainText = "";
string str = "";
if (d == 0 || n == 0)
{
io.writeConsole("Keys is missing!!!");
return plainText;
}
io.writeLog("[RSA::decrypt] decrypting...");
for (char character : cipherText)
{
if (character != ' ')
str += character;
else
{
//io.writeLog("[RSA::decrypt] decrypt " + str);
BigInt c;
if (base == BigIntBase::BASE_10)
c = converter.decimalStrToBigInt(str);
else if (base == BigIntBase::BASE_2)
c = converter.binaryStrToBigInt(str);
BigInt m = powMod(c, d, n);
//io.writeLog("[RSA::encrypt] ascii value as BigInt: ", m, base);
int ascii = m.getIntValue();
io.writeLog("[RSA::encrypt] ascii value: " + to_string(ascii));
plainText += char(ascii);
str = ""; //? Reset chuỗi nhị phân
}
}
io.writeLog("[RSA::decrypt] finish decrypting: " + plainText);
return plainText;
}
void RSA::encryptFile(string plainTextFile, string cipherTextFile)
{
io.writeConsole("Encrypting " + plainTextFile);
fstream pfs, cfs;
if (io.openFile(pfs, plainTextFile, ios::in) == false) return;
if (io.openFile(cfs, cipherTextFile, ios::out) == false) return;
string plainText = io.readFile(pfs);
string cipherText = encrypt(plainText);
cfs << cipherText;
pfs.close();
cfs.close();
io.writeConsole("Finish encrypting!");
io.writeConsole("Press any key to continue...");
system("pause>0");
}
void RSA::decryptFile(string cipherTextFile, string plainTextFile)
{
io.writeConsole("Decrypting " + cipherTextFile);
fstream cfs, pfs;
if (io.openFile(cfs, cipherTextFile, ios::in) == false) return;
if (io.openFile(pfs, plainTextFile, ios::out) == false) return;
string cipherText = io.readFile(cfs);
string plainText = decrypt(cipherText);
pfs << plainText;
cfs.close();
pfs.close();
io.writeConsole("Finish decrypting!");
io.writeConsole("Press any key to continue...");
system("pause>0");
}
string RSA::getKeys()
{
string keys;
if (base == BigIntBase::BASE_10)
{
keys += "n: " + converter.bigIntToDecimalStr(n) + '\n';
keys += "e: " + converter.bigIntToDecimalStr(e) + '\n';
keys += "d: " + converter.bigIntToDecimalStr(d) + '\n';
keys += "phi: " + converter.bigIntToDecimalStr(phi) + '\n';
}
else if (base == BigIntBase::BASE_2)
{
keys += "n: " + converter.bigIntToBinaryStr(n) + '\n';
keys += "e: " + converter.bigIntToBinaryStr(e) + '\n';
keys += "d: " + converter.bigIntToBinaryStr(d) + '\n';
keys += "phi: " + converter.bigIntToBinaryStr(phi) + '\n';
}
return keys;
}