-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZKRY2.cpp
More file actions
409 lines (343 loc) · 11.6 KB
/
ZKRY2.cpp
File metadata and controls
409 lines (343 loc) · 11.6 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "config.h"
//sifrovania
string to_lower(string input) {
string result;
for (int i = 0; i<input.length(); i++) {
char tmp = input[i];
if (tmp > 64 && tmp < 91) tmp += 32;
result+=tmp;
}
return result;
}
int val(char c) {
return c - 97;
}
int ival(char c) {
return c + 97;
}
int func(int x, int a, int b, int c) {
return (x * a + b) % c;
}
int ifunc(int x, int a, int b, int c) {
int tmp = x - b;
while(tmp % a != 0)
tmp += c;
return tmp / a;
}
#define ot val(OT[i])
#define zt val(ZT[i])
#define in a, b, c
#define asd string OT, string& ZT, int &IV, int a, int b, int c, int i
void _ecb(asd) { ZT += ival(func(ot, in)); }
void _cbc(asd) { ZT += ival(func(ot + zt, in)); }
void _cfb(asd) { ZT += ival(func(func(zt, in), 1, ot, c)); }
void _ofb(asd) { IV = func(IV, in); ZT += ival(func(ot, 1, IV, c)); }
void _ctr(asd) { IV = func(IV + i + 1, in); ZT += ival(func(ot, 1, IV, c)); }
void(*sifrovat[5])(asd) = {_ecb, _cbc, _cfb, _ofb, _ctr};
#define fgh string& OT, string ZT, int& IV, int a, int b, int c, int i
void _ecb(fgh) { OT += ival(ifunc(zt, in)); }
void _cbc(fgh) { OT += ival(func(ifunc(zt, in) - val(ZT[i + 1]), 1, c, c)); }
void _cfb(fgh) { OT += ival(func(zt - func(val(ZT[i + 1]), in), 1, c, c)); }
void _ofb(fgh) { IV = func(IV, in); OT += ival(func(zt - IV, 1, c, c)); }
void _ctr(fgh) { IV = func(IV + i + 1, in); OT += ival(func(zt - IV, 1, c, c)); }
void(*desifrovat[5])(fgh) = { _ecb, _cbc, _cfb, _ofb, _ctr };
enum mode { ecb, cbc, cfb, ofb, ctr };
mode input0() {
int tmp;
cout << "Aky mod chces pouzit?: 1-ecb, 2-cbc, 3-cfb, 4-ofb, 5-ctr: ";
cin >> tmp;
return mode(tmp - 1);
}
string input1(string input) {
string text;
cout << "Zadaj " << input << ": ";
cin >> text;
text = to_lower(text);
for (int i = 0; i < text.length(); i++)
cout << val(text[i]) << " ";
return text;
}
void input2(int& a, int& b, int& c) {
cout << "Zadaj parametre (a b c) funkcie f pre Ax+b mod c:";
cin >> a >> b >> c;
cout << a << "x + " << b << " mod " << c << endl;
}
#define params OT, ZT, IV, in, i
void sifruj() {
mode m = input0();
string OT = input1("OT");
int IV=0, in;
if (m != ecb) {
cout << "Zadaj hodnotu IV: ";
cin >> IV;
}
input2(in);
string ZT;
if(m == cbc || m == cfb) ZT += ival(func(IV, in));
for (int i = 0; i < OT.length(); i++)
sifrovat[m](params);
cout << ZT << endl;
}
void desifruj() {
mode m = input0();
string ZT = input1("ZT");
if (m == cbc || m == cfb) reverse(ZT.begin(), ZT.end());
int IV=0, in;
if (m == ofb || m == ctr) {
cout << "Zadaj hodnotu IV: ";
cin >> IV;
}
input2(in);
string OT;
if (m == cbc || m == cfb) {
for (int i = 0; i < ZT.length()-1; i++)
desifrovat[m](params);
IV = ifunc(val(ZT[ZT.length() - 1]), in);
reverse(OT.begin(), OT.end());
cout << endl << "OT: " << OT << endl << "IV: " << IV << endl;
}
else {
for (int i = 0; i < ZT.length(); i++)
desifrovat[m](params);
cout << OT << endl;
}
}
#undef in
void RSA_sifruj() {
int n, e, x;
cout << "Zadaj VK(n,e) a spravu x:";
cin >> n >> e >> x;
ZZ_p::init(to_ZZ(n));
ZZ_p res = to_ZZ_p(power(to_ZZ(x), e));
cout << "E(x)= x^e mod n\nE(x)= "<< x << "^" << e <<" mod "<< n << " = " << res << endl;
}
void RSA_desifruj() {
int n, d, y;
cout << "Zadaj PK(n,d) a spravu y:";
cin >> n >> d >> y;
ZZ_p::init(to_ZZ(n));
ZZ_p res = to_ZZ_p(power(to_ZZ(y), d));
cout << "D(y)= y^d mod n\nD(y)= " << y << "^" << d << " mod " << n << " = " << res << endl;
}
void RSA_2020() {
int n, e, x;
cout << "pre VK(n,e) a msg x, zadaj n,e,x:";
cin >> n >> e >> x;
vec_ZZ list = factorize_number(to_ZZ(n));
cout << " hodnota n( " << n << " ) rozlozena na prvociselne delitele : " << list << endl;
if (list.length() > 2) return;
int max_power = cancer(list);
cout << " lambda( " << n << " ) = lcm( ";
for (int i = 0; i < list.length(); i++) {
cout << " lam( " << list[i] << " )";
if (i != list.length() - 1) cout << ", ";
}
int p, q; //89,97
p = to_long(list[0]);
q = to_long(list[1]);
vec_to_lambda(list);
cout << " ) = lcm( ";
for (int i = 0; i < list.length(); i++) {
cout << list[i];
if (i != list.length() - 1) cout << ", ";
}
//toto funguje iba ak n je naozaj cislo ktore ma prave dva prvociselne delitele
int tmp, d, d1, d2, y1, y2, x1, x2, u, v,res;
tmp = lcm(list);
d = InvMod(e, tmp);
d1 = d % (p-1);
d2 = d % (q-1);
y1 = x % p;
y2 = x % q;
x1 = PowerMod(y1, d1, p);
x2 = PowerMod(y2, d2, q);
u = InvMod(q, p);
v = InvMod(p, q);
res = AddMod(x1 * u * q, x2 * v * p, n);
cout << " ) = " << tmp << endl
<< " " << e << " x d = " << "1 (mod " << tmp << ") -> d = " << e << "^-1(inverzny prvok) -> d = " << d << endl
<<"Algorytmus rychleho desifrovania:"<<endl
<< "d1= " << d << "(mod " << p-1 << ") = " << d1 << endl
<< "d2= " << d << "(mod " << q-1 << ") = " << d2 << endl
<< "y1= " << x << "(mod " << p << ") = " << y1 << endl
<< "y2= " << x << "(mod " << q << ") = " << y2 << endl
<< "x1= " << y1 << "^" << d1 << "(mod " << p << ") = " << x1 << endl
<< "x2= " << y2 << "^" << d2 << "(mod " << q << ") = " << x2 << endl
<< "u= " << q << "^-1" << "(mod " << p << ") = " << u << endl
<< "v= " << p << "^-1" << "(mod " << q << ") = " << v << endl
<< "result-> (x1 * u * q + x2 * v * p) mod n"<<endl
<< "( " << x1 << " * " << u << " * " << q << " + " << x2 << " * " << v << " * " << p << " ) mod " << n << " = " << res % n << endl;
}
int char_to_int(char c , string table, int offset) {
for (int i = 0; i < table.length(); i++)
if (c == table[i]) return i+offset;
}
void El_gamal_2020() {
ZZ p, a, c;
cout << "Zadaj VK(p,a,c): ";
cin >> p >> a >> c;
string ZT;
cout << "Zadaj ZT: ";
cin >> ZT;
string table;
cout << "Zadaj prekladovu tabulku: ";
cin >> table;
int offset;
cout << "Zadaj offset zaciatku indexovania: ";
cin >> offset;
int y1, y2, b, A, B, OT;
b = ord(a, p, c);
cout << "Ak pozname b tak mozeme desifrovat podla vzorca x = y2 * (y1^b)^-1 mod p, kde y1,y2 je ZT a x je OT\n";
cout << "b je :" << b << endl;
cout << "_________________________________________\n"
<< "| | A | B | |\n"
<< "| ZT=(y1,y2) | y1^b | A^-1 | y2 * B | OT |\n";
for (int i = 0; i < ZT.length(); i+=2) {
char tmp = ZT[i];
char tmp2 = ZT[i + 1];
y1 = char_to_int(tmp, table, offset);
y2 = char_to_int(tmp2, table, offset);
A = PowerMod(y1, b, to_long(p));
B = InvMod(A, to_long(p));
OT = MulMod(B, y2, to_long(p));
cout << "|(" << tmp << "," << tmp2 << ")=(" << y1 << "," << y2 << ")| " << A << " | " << B << " | " << OT << " | " << table[OT - offset] << " |\n";
}
}
int inverse(ZZ A, ZZ M) {
A = ((A % M)+M)%M;
for (long i = 1; i < M; i++)
if ((A*i)%M == 1) return i;
return 0;
}
void vypocet(ZZ a, ZZ p) {
ZZ x1, x2, y1, y2, lam, tmp;
cout << "zadaj P(x1,y1): ";
cin >> x1 >> y1;
cout << "zadaj Q(x2,y2): ";
cin >> x2 >> y2;
tmp = y1 + y2;
if (x1 == x2 && tmp == p || tmp == 0) {
cout << "P + Q = 0";
return;
}
if (x1 == x2 && y1 == y2) {
lam = rep(to_ZZ_p((3 * x1 * x1 + a) * inverse(2 * y1, p)));
cout << "lam= (3 * x1^2 + a) * (2 * y1)^-1 mod p\n";
}
else {
lam = rep(to_ZZ_p((y2 - y1) * inverse(x2 - x1, p)));
cout << "lam= (y2 - y1) * (x2 - x1)^-1 mod p\n";
}
ZZ x3 = rep(to_ZZ_p(lam * lam - x1 - x2));
ZZ y3 = rep(to_ZZ_p(lam * (x1 - x3) - y1));
cout << "lam = " << lam << endl
<< "x3= lam^2 - x1 - x2"<<endl
<< "x3= "<<x3<<endl
<< "y3= lam * (x1 - x3) - y1"<<endl
<< "y3= "<<y3<<endl
<< "P(" << x1 << "," << y1 << ") + Q(" << x2 << "," << y2 << ") = (" << x3 << "," << y3 << ")"<<endl;
}
string addd(ZZ num) {
string s="";
if(num==0) s = (char)to_long((num % 10) + 48) + s;
else
while (num > 0) {
s = (char)to_long((num % 10) + 48) + s;
num /= 10;
}
return s;
}
void addd(string &out,ZZ x, ZZ y) {
out = out + "| (" + addd(x) + "," + addd(y) + ")";
}
#define xi x[i]
#define xj x[j]
#define yi y[i]
#define yj y[j]
void table(ZZ a, ZZ p, vec_ZZ x, vec_ZZ y) {
string out = "";
for (int i = -1; i < 5; i++, out += "\n")
for (int j = -1; j < 5; j++)
if (i == -1 && j == -1) out += " + ";
else if (i == -1) addd(out, xj, yj);
else if (j == -1) addd(out, xi, yi);
else if (xi == xj && (yi + yj) == p || (yi + yj) == 0) out += " 0 ";
else {
ZZ lam = (yj - yi) * inverse(xj - xi, p);
if (xi == xj && yi == yj)
lam = (3 * xi * xi + a) * inverse(2 * yi, p);
ZZ x3 = rep(to_ZZ_p(lam * lam - xi - xj));
ZZ y3 = rep(to_ZZ_p(lam * (xi - x3) - yi));
addd(out, x3, y3);
}
cout << out;
}
void epilepticke_krivky_2020() {
ZZ a, b, p;
cout << "Zadaj parametre (a,b,p) pre krivku y^2 = x^3 + ax + b mod p: ";
cin >> a >> b >> p;
if (AddMod(PowerMod((4 * a) % p, 3, p),PowerMod((27 * b) % p, 2, p),p) == 0) {
cout << "Zadane parametre netvoria elipticku krivku.\n";
return;
}
cout << "Zadane parametre tvoria elipticku krivku.\n";
vec_ZZ x, y;
ZZ_p::init(p);
for (ZZ i(0); i < p; i++)
for (ZZ j(0); j < p; j++)
if (to_ZZ_p(j*j) == to_ZZ_p(i*i*i) + to_ZZ_p(a * i) + to_ZZ_p(b)) {
x.append(i);
y.append(j);
}
int dlzka = x.length();
if (dlzka > 20) dlzka = 20;
for (int i = 0; i < dlzka; i++)
cout << "Bod "<<i+1<<" x, y = [" << xi << ", " << yi << "]" << endl;
table(a, p, x, y);
while (1) {
cout << "Stlac 0 pre ukoncenie prikladu\nStlac 1 pre vypocet konkretneho suctu:";
bool tmp;
cin >> tmp;
if (tmp == 0) return;
else if (tmp == 1) vypocet(a, p);
}
}
bool is_prime(ZZ num) {
ZZ sqrt = SqrRoot(num);
cout << "testing if " << num << " is prime\n"
<< "sqrt: " << sqrt << endl;
if (rem(num, 2) == 0) return 0;
for (int i = 3; i < sqrt; i+=2)
if (rem(num, i) == 0) return 0;
return 1;
}
void Gordon_Helman_Bach_2020() {
ZZ s, t, b;
cout << "Zadaj parametre s,t a pocet bitov b: ";
cin >> s >> t >> b;
ZZ tmp(b/2), q, p;
for (int i = 1; i < tmp; i++) {
q = 2 * i * t + 1;
cout << "i: "<< i << ",q: " << q << endl;
if (is_prime(q)) break;
}
ZZ p0 = 2 * PowerMod(s, q - 2, q) * s - 1;
cout << "p0= 2 * (s^(q-2) mod q) * s -1" << endl
<< "p0= 2 * (" << s << "^(" << q - 2 << ") mod " << q << ")* " << s << " - 1" << endl
<< "p0=" << p0 << endl;
for (int i = 0; i < tmp; i++) {
p = p0 + 2 * i * q * s;
cout << "i: " << i << ",p: " << p << endl;
if (is_prime(p)) break;
}
cout << "Cislo p(" << p << ") je silne prvocislo s " << b << " alebo viac bitmi." << endl;
}
void (*ZKRY2[8])() = { sifruj,
desifruj,
RSA_2020,
El_gamal_2020,
epilepticke_krivky_2020,
Gordon_Helman_Bach_2020,
RSA_sifruj,
RSA_desifruj
};