-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrustCenter.h
80 lines (69 loc) · 1.51 KB
/
TrustCenter.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
#include <iostream>
#include <cmath>
#include <string>
#include <random>
#include "BigInteger/BigInteger.h"
using namespace std;
class TrustCenter {
public:
int p, q, g, y, w;
void GenKeys() {
printf("Trust Center is generating keys...\n");
BigInteger temp;
srand(time(0));
bool Check;
do {
p = rand() % 500 + 5;
Check = SimpleNumCheck(p);
} while (!Check);
Check = false;
do {
q = rand() % 500 + 5;
if (mod(p - 1, q) == 0) {
Check = true;
}
} while (!Check);
Check = false;
do {
g = rand() % 500 + 5;
temp = Bigpow(g, q);
if (mod(temp, p) == 1) {
Check = true;
}
} while (!Check);
Check = false;
w = rand() % (q - 1) + 1;
temp = Bigpow(g, w);
temp = reverse(temp, p);
y = stoi(temp.getNumber());
printf("\nOpen keys are {%d,%d,%d,%d}, Secret key is {%d}\n", p, q, g, y, w);
}
private:
BigInteger Bigpow(int base, int pow) {
BigInteger result = 1;
for (int i = 1; i < pow;i++) {
result *= base;
}
return result;
}
BigInteger mod(BigInteger x, int N) {
return ((x % N + N) % N);
}
int reverse(BigInteger a, int q) { //Обратное число по модулю
mod(a, q);
BigInteger i;
for (i = 1;i < a;i++)
if (mod((a * i), q) == 1) return stoi(i.getNumber());
return 99999;
}
bool SimpleNumCheck(BigInteger num) { //Проверка числа на простоту
for (BigInteger z = 2; stoi(z.getNumber()) <= sqrt(stoi(num.getNumber())); z++)
{
if (num % z == 0)
{
return false;
}
}
return true;
}
};