-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.cpp
228 lines (182 loc) · 4.2 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
//
// Created by Sankalp Bhamare on 20/11/19.
//
#include "RSA.h"
using namespace std;
const int saltLen = 3;
long int gcd(long int a, long int b) {
long int t;
while (true) {
t = a % b;
if (t == 0)
return b;
a = b;
b = t;
}
}
long int newPrime(long int x) {
long int n = 0;
long int prime = 0;
long int i = 0;
while (n < x) {
if (isPrime(i)) {
n++;
prime = i;
}
i++;
}
return prime;
}
bool isPrime(long int x) {
for (long int i = 2; i < sqrt(x); i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
RSAGenerator::RSAGenerator() {
}
RSAGenerator::RSAGenerator(long int salt) {
this->salt = salt;
}
bool RSAGenerator::checkSalt() {
if (salt > ((long int) pow(10, saltLen * 2 - 1)) && salt < ((long int) pow(10, saltLen * 2))) {
if (p1 == p2) {
return false;
} else if (p1 < 10 || p2 < 10) {
return false;
} else
return true;
} else {
return false;
}
}
void RSAGenerator::useSalt() {
if (salt > ((long int) pow(10, saltLen * 2 - 1)) && salt < ((long int) pow(10, saltLen * 2))) {
p1 = salt % ((int) pow(10, saltLen));
p2 = (salt - p1) / ((int) pow(10, saltLen));
}
}
void RSAGenerator::createKey() {
useSalt();
if (checkSalt()) {
p = newPrime(p1);
q = newPrime(p2);
n = p * q;
phi = (p - 1) * (q - 1);
createPublicKey();
createPrivateKey();
}
}
void RSAGenerator::createPublicKey() {
e = 7;
long int ea[100];
long int er = 7;
long int track;
long int ind = 0;
while (er < phi) {
if (ind >= 100)
break;
track = gcd(er, phi);
if (track == 1 && er != p && er != q) {
ea[ind++] = er;
}
er++;
}
e = ea[ind / 2];
}
void RSAGenerator::createPrivateKey() {
long int i = 1;
while (true) {
if ((1 + i * phi) % e == 0) {
break;
}
i++;
}
d = (1 + i * phi) / e;
}
RSAKey::RSAKey() {
n = 0;
e = 0;
d = 0;
isPrivate = false;
}
RSAKey::RSAKey(RSAGenerator rgen) {
n = rgen.n;
e = rgen.e;
d = rgen.d;
isPrivate = true;
}
long int RSAKey::encrypt(long int data) {
long int res = 1;
for (long int i = 0; i < e; i++) {
res = res * data;
res = res % n;
}
return res;
}
long int RSAKey::decrypt(long int data) {
if (isPrivate) {
long int res = 1;
for (long int i = 0; i < d; i++) {
res = res * data;
res = res % n;
}
return res;
} else {
return -1;
}
}
void RSAKey::saveToFile(char *fileName) {
ofstream fout(fileName, ios::binary | ios::out);
fout.write((char *) this, sizeof(RSAKey));
fout.close();
}
void RSAKey::savePublicKey(char *fileName) {
ofstream fout(fileName, ios::binary | ios::out);
long int temp = this->d;
this->d = 0;
this->isPrivate = false;
fout.write((char *) this, sizeof(RSAKey));
this->d = temp;
this->isPrivate = true;
fout.close();
}
int RSAKey::loadFromFile(char *fileName) {
ifstream fin(fileName, ios::binary | ios::in);
if(!fin){
return 0;
}
fin.read((char *) this, sizeof(RSAKey));
fin.close();
return 1;
}
RSABlockKey::RSABlockKey(RSAKey key) {
this->key = key;
}
void RSABlockKey::encrypt(char *blob, long int size, long int *res) {
for (long int i = 0; i < size; i++) {
res[i] = key.encrypt(blob[i]);
}
}
void RSABlockKey::decrypt(long int *blob, long int size, char *res) {
for (long int i = 0; i < size; i++) {
res[i] = (char) key.decrypt(blob[i]);
}
}
RSABlockKey::RSABlockKey() {
}
void RSABlockKey::changeKey(RSAKey key) {
this->key = key;
}
void RSABlockKey::writeBlockData(char *blob, long int size, std::ostream &fout) {
long int res[size];
encrypt(blob, size, res);
fout.write((char*)&res,sizeof(res));
}
void RSABlockKey::readBlockData(char *blob, long int size, std::istream &fin) {
long int res[size];
fin.read((char*)&res,sizeof(res));
decrypt(res, size, blob);
}