-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.h
360 lines (332 loc) · 9.33 KB
/
RSA.h
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**********************************************************************
* RSA library.
* version: 1.5
*
* July, 5th, 2015
*
* This lib was written by DucThang
* Contact:[email protected]
*
* Every comment would be appreciated.
*
* If you want to use parts of any code of mine:
* let me know and
* use it!
**********************************************************************
generate_RSA_Key("thang.pem");
gen_public_RSA_Key("thang.pem","thang1.pem");
decryptRSA(fileText,file RSA(pub/pri),fileOuput);
encryptRSA(fileText,file RSA(pub/pri),fileOuput);
**********************************************************************/
#ifndef RSA_H
#define RSA_H
#include "../PrGlib.h"
#include "Base64.h"
#include "ASN1.h"
#include <NTL/ZZ.h>
#include <gmpxx.h>
#include <string>
#include <iostream>
#include <time.h>
#include <fstream>
using namespace std;
using namespace NTL;
#define KEY 128
typedef ZZZ mpz_class;
string convert_hexa(string a)
{
string b;
ZZZ ma;
for(int i=0;i<a.length();i+=8)
{
string maa=a.substr(i,8);
if(maa.length()!=8)break;
ma.set_str(maa,2);
maa=ma.get_str(16);
if(maa.length()==1)maa="0"+maa;
b+=maa;
}
return b;
}
void generate_RSA_Key(char *filePrivateKey)
{
PrGlib thang;
thang.PrG_set_size(3072);
thang.PrG_renew();
/*Generate p and q as strong primes */
ZZZ p=thang.PrG_generate_strong_prime();
thang.PrG_renew();
ZZZ q=thang.PrG_generate_strong_prime();
if(p>q)
{
ZZZ ma=p;
p=q;
q=ma;
}
ZZZ n=q*p;
ZZZ phi=(p^1)*(q^1);
ZZZ e=65537;
/*Find e such that gcd(e,phi)=1*/
/*******************************/
/*Compute d= e^-1 mod n*/
ZZZ d;
mpz_invert(d.get_mpz_t(),e.get_mpz_t(),phi.get_mpz_t());
//PEM
RSA_Key keys;
keys.modulu_n=n;
keys.Version=0;
keys.prime_p=p;
keys.prime_q=q;
keys.privateExponent_d=d;
keys.publicExponent_e=e;
keys.exponent_p=d%(p-1);
keys.exponent_q=d%(q-1);
mpz_invert(keys.coefficient.get_mpz_t(),p.get_mpz_t(),q.get_mpz_t());
//(inverse of q) mod p
ZZZ xua;
xua.set_str(privatePEMEN(keys),16);
ofstream pem(filePrivateKey);
string a=base64_encode("00"+xua.get_str(2));
pem<<"-----BEGIN RSA PRIVATE KEY-----"<<endl;
for(int i=1;i<=a.length();i++)
{
pem<<a[i-1];
if(i%64==0&&i!=0&&i!=a.length())pem<<endl;
}
pem<<endl<<"-----END RSA PRIVATE KEY-----";
pem.close();
}
void gen_public_RSA_Key(char * filepem,char *filename)
{
ifstream pem(filepem);
if(!filepem)return;
string code,a;
while(!pem.eof())
{
getline(pem,a);
if(a=="-----BEGIN RSA PRIVATE KEY-----")
{ getline(pem,a);
do{
code+=a;
getline(pem,a);
}while(a!="-----END RSA PRIVATE KEY-----");
}
}
code=base64_decode(code);
code=convert_hexa(code);
RSA_Key b=privatePEMDE(code);
/*************************************/
ZZZ xua;xua.set_str(publicPEMEN(b),16);
ofstream pem1(filename);
a=base64_encode("00"+xua.get_str(2));
pem1<<"-----BEGIN PUBLIC KEY-----"<<endl;
for(int i=1;i<=a.length();i++)
{
pem1<<a[i-1];
if(i%64==0&&i!=0&&i!=a.length())pem1<<endl;
}
pem1<<endl<<"-----END PUBLIC KEY-----";
pem1.close();
}
void decryptRSA (char* fileText,char* RSAKey,bool isPrivateKey, char *fileDecrypt)
{
ifstream filetext(fileText);
ofstream decrypt(fileDecrypt);
ifstream pem(RSAKey);
if(!pem){
cout<<"Fail!publicKey.txt isn't exist";
return;
}
if(!filetext){
cout<<"Fail!filetext.txt isn't exist";
return;
}
if(isPrivateKey)
{
string code,a;
while(!pem.eof())
{
getline(pem,a);
if(a=="-----BEGIN RSA PRIVATE KEY-----")
{ getline(pem,a);
do{
code+=a;
getline(pem,a);
}while(a!="-----END RSA PRIVATE KEY-----");
}
}
code=base64_decode(code);
code=convert_hexa(code);
RSA_Key b=privatePEMDE(code);
/**********************/
/*read text from fileText*/
//filetext>>a;
string file;
while(!filetext.eof())
{
getline(filetext,a);
file+=a;
}
ZZZ codes;
/*decrypto base64 to bits*/
codes.set_str(base64_decode(file),2);
/*M=C^d mode n*/
mpz_powm(codes.get_mpz_t(),codes.get_mpz_t(),b.privateExponent_d.get_mpz_t(),b.modulu_n.get_mpz_t());
/*convert to bit and crop length of KEY{128,192,256}*/
a=codes.get_str(2);
a=a.substr(2048-KEY,KEY);
/********************/
codes.set_str(a,2);
decrypt<<codes.get_str();
}else{
string code,a;
while(!pem.eof())
{
getline(pem,a);
if(a=="-----BEGIN PUBLIC KEY-----")
{ getline(pem,a);
do{
code+=a;
getline(pem,a);
}while(a!="-----END PUBLIC KEY-----");
}
}
code=base64_decode(code);
code=convert_hexa(code);
RSA_Public_Key b=publicPEMDE(code);
/**********************/
string file;
while(!filetext.eof())
{
getline(filetext,a);
file+=a;
}
ZZZ codes;
/*decrypto base64 to bits*/
codes.set_str(base64_decode(file),2);
/*M=C^d mode n*/
mpz_powm(codes.get_mpz_t(),codes.get_mpz_t(),b.publicExponent_e.get_mpz_t(),b.modulu_n.get_mpz_t());
/*convert to bit and crop length of KEY{128,192,256}*/
a=codes.get_str(2);
a=a.substr(2048-KEY,KEY);
/********************/
codes.set_str(a,2);
decrypt<<codes.get_str();
}
filetext.close();
decrypt.close();
pem.close();
}
void cryptRSA (char* fileText,char* RSAKey,bool isPrivateKey,char *fileCrypt,string headCode="1000010010000100")
{
ifstream filetext(fileText);
ifstream pem(RSAKey);
ofstream filecrypt(fileCrypt);
if(!filetext){
cout<<"Fail!Filetext isn't exist";
return;
}
if(!pem){
cout<<"Fail!Filekey isn't exist";
return;
}
/*read key from fileKey*/
if(isPrivateKey)
{
string code,a;
while(!pem.eof())
{
getline(pem,a);
if(a=="-----BEGIN RSA PRIVATE KEY-----")
{ getline(pem,a);
do{
code+=a;
getline(pem,a);
}while(a!="-----END RSA PRIVATE KEY-----");
}
}
code=base64_decode(code);
code=convert_hexa(code);
RSA_Key b=privatePEMDE(code);
/*linked head16bit with bit random + KEY{128,192,256}*/
string TEXT=headCode;
ZZ numbersite;
RandomBits(numbersite,(2032-KEY));
stringstream buffer;
buffer<<numbersite;
ZZZ codes;
codes=buffer.str();
int strlen=codes.get_str(2).length();
for(;strlen<2032-KEY;strlen++)TEXT+='1';
TEXT+=codes.get_str(2);
filetext>>headCode;
codes=headCode;
TEXT+=codes.get_str(2);
/*********************/
/*C=M^e mod n*/
codes.set_str(TEXT,2);
mpz_powm(codes.get_mpz_t(),codes.get_mpz_t(),b.privateExponent_d.get_mpz_t(),b.modulu_n.get_mpz_t());
string ba=codes.get_str(2);
int leng=ba.length()%6;
for(int i=0;i<6-leng;i++)
{
ba='0'+ba;
}
ba=base64_encode(ba);
for(int i=1;i<=ba.length();i++)
{
filecrypt<<ba[i-1];
if(i%64==0&&i!=0&&i!=ba.length())filecrypt<<endl;
}
}else{
string code,a;
while(!pem.eof())
{
getline(pem,a);
if(a=="-----BEGIN PUBLIC KEY-----")
{ getline(pem,a);
do{
code+=a;
getline(pem,a);
}while(a!="-----END PUBLIC KEY-----");
}
}
code=base64_decode(code);
code=convert_hexa(code);
RSA_Public_Key b=publicPEMDE(code);
/*linked head16bit with bit random + KEY{128,192,256}*/
string TEXT=headCode;
ZZ numbersite;
RandomBits(numbersite,(2032-KEY));
stringstream buffer;
buffer<<numbersite;
ZZZ codes;
codes=buffer.str();
int strlen=codes.get_str(2).length();
for(;strlen<2032-KEY;strlen++)TEXT+='1';
TEXT+=codes.get_str(2);
filetext>>headCode;
codes=headCode;
TEXT+=codes.get_str(2);
/*********************/
/*C=M^e mod n*/
codes.set_str(TEXT,2);
mpz_powm(codes.get_mpz_t(),codes.get_mpz_t(),b.publicExponent_e.get_mpz_t(),b.modulu_n.get_mpz_t());
string ba=codes.get_str(2);
int leng=ba.length()%6;
for(int i=0;i<6-leng;i++)
{
ba='0'+ba;
}
ba=base64_encode(ba);
for(int i=1;i<=ba.length();i++)
{
filecrypt<<ba[i-1];
if(i%64==0&&i!=0&&i!=ba.length())filecrypt<<endl;
}
}
filecrypt.close();
filetext.close();
pem.close();
}
#endif // RSA_H