-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKey_generate.h
317 lines (273 loc) · 6.5 KB
/
Key_generate.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
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include <iterator>
#include <cmath>
#ifndef Key_generate_H
#define Key_generate_H
using namespace std;
class Key_generate
{
private:
int p, q;
int fact_c=1, can;
int old_fact_c;
int get_p_Prime()
{
return p;
}
int get_q_Prime()
{
return q;
}
// Returns gcd of a and b
int gcd(int a, int b)
{
while (1)
{
int temp = a % b;
if (temp == 0)
return b;
a = b;
b = temp;
}
}
// Get the prime factors of n
void primeFactors(int n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
cout << 2 <<" ";
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
cout << i <<" ";
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
{
cout << n <<" ";
}
}
void Key1()
{
int ap=1;
Miller_Rabin prime;//prime no can't be factorized so excluded
for(int i=1; i <= can; i++)
{
if(((etf*i) + 1)%etf==1 && !prime.isPrime((etf*i)+1,1000))
{
cout<<ap<<". Factors of K="<<(etf*i)+1<<": "; ap++;
primeFactors((etf*i)+1); cout<<endl;
}
else
can++;
}
}
bool Key2(int e, int d)
{
if (gcd(etf, e) == 1 && gcd(etf, d) == 1 && (e*d) % etf==1)
return true;
else
return false;
}
public:
vector<int> e_data;
vector<int> d_data;
int n, etf, stat;
int e, d;
void setPrimes(int P, int Q)
{
p = P;
q = Q;
}
int get_e()
{
return e;
}
int get_d()
{
return d;
}
int Calc_n(int p, int q)
{
n = p * q;
return n;
}
int Calc_etf(int p, int q)
{
etf = (p - 1) * (q - 1);
return etf;
}
void Cacl_keys()
{
cout<<"Creating canditates such as K equals to 1 mod E(n). The factors of K most likely are the values of e and d."<<endl;
cout<<"How many canditates would you like to generate? (5-30): "; cin>>can;
cout<<"\n(EXPERIMENTAL) Would you like to assign e and d values automatically? (y/n): ";
string s=""; cin>>s;
if(s=="y")
{
cout<<"Assigning e and d automatically . . . "<<endl;
stat = 1;
Key1();
}
else
{
Key1();
cout<<"\nUsing the factorization list above, pick two numbers to assign e and d from K. ";
cout<<"In case there are more than 2 factors for a single K, either find another K or multiply the factors until you remain with only 2. "<<endl;
cout<<"e = ";
cin>>e;
cout<<"d = ";
cin>>d;
}
while(!Key2(e, d))
{
cout<<"\nOne of the e and d values must be typed wrong or it doesn't correspond to the same K. Try again!"<<endl;
cout<<"e = ";
cin>>e;
cout<<"d = ";
cin>>d;
if (Key2(e, d))
break;
}
if (Key2(e, d))
{
//cout<<"e = "<<e<<endl;
//cout<<"d = "<<d<<endl;
cout<<"n = "<<n<<endl;
cout<<"E(n) = "<<etf<<endl;
cout<<"e*d = "<<e*d<<endl;
cout<<"e*d mod E(n) = "<<(e*d)%etf<<endl;
cout<<"e and E(n) are relatively prime"<<endl;
cout<<"d and E(n) are relatively prime"<<endl;
}
//Encrypt_Decrypt msg;
//msg.setKeys(e, d);
//msg.set_n(p*q);
}
};
#endif
/*
vector<int> fact (50);
int* p = fact.data();
int fact_size = fact.size();
old_fact_c = fact_c;
*p=2;++p;//fact.at(fact_c)=2;
fact_c++;
//fact_pos(fact,fact_c);fact.at(fact_c)
if(stat != 1)
//fact_c = fact_c - old_fact_size + 1; fact_c = fact_size - fact_c - 1;
//cout<<"\nfact_c "<<fact_c<<endl; cout<<"fact_size "<<fact.size()<<endl;
//int fact_dc= fact_c - old_fact_c;
//int act_size = fact_c % fact_size;
//cout<<"act_size "<<act_size<<endl;
/* if(stat==1)
for(int c=1; c <= act_size; c++)
{
if(act_size == 2)
cout << fact.at((fact_c - act_size)+c) <<" ";
else if(act_size > 2)
{
int in, af, result;
in = fact.at((fact_c - act_size)+c);
af = fact.at(((fact_c - act_size)+c)+1);
result = in * af;
fact.at((fact_c - act_size)+c) = result;
cout << fact.at(((fact_c - act_size)+c)) <<" ";
act_size--;
}
}
//iota(fact.begin(), fact.end(), 100);
//fact.at(fact_c)=i;
//fact_pos(fact,fact_c);
*p=n;++p;//fact.at(fact_c)=n;
fact_c++;
if(stat != 1)
*/
/*void fact_pos(vector <int> const &a, int c)
{
while(a.size() < c)
//a.push_back(c);
//for(int i=0; i < a.size(); i++)
cout << a.at(c) <<" * "<< ' ';
}*/
// A function to print all prime
// factors of a given number n
//in class example
/*int k = etf + 1;
for (e=2; e < etf; e++)
{
//cout<<"etf"<<etf<<endl;
//cout<<"e"<<e<<endl;
//cout<<"gcd"<<gcd(etf, e)<<endl;
if (gcd(etf, e) == 1 && gcd(n, e) == 1)
{
double d = ((etf*i) + 1) / e;
int ed = e * d;
if(floor(d)/d == 1 && gcd(etf, d) == 1)
{
d=(int)d;
if(ed % etf == 1 && ed == k)
{
E = d;
D = e;
}
}
}
}
}*/
/*void Key_public()
{
for(int i=0; i < fact.size(); i++)
cout <<"i="<<i<<"vector pos="<< fact.at(i) << ' ';
// Private key (d stands for decrypt)
// choosing d such that it satisfies
// d*e = 1 + k * totient
int k = 2; // A constant value
double d = (1 + (k * phi)) / e;
// Message to be encrypted
double msg = 88;
printf("Message data = %lf", msg);
// Encryption c = (msg ^ e) % n
double c = pow(msg, e);
c = fmod(c, n);
printf("\nEncrypted data = %lf", c);
// Decryption m = (c ^ d) % n
double m = pow(c, d);
m = fmod(m, n);
printf("\nOriginal Message Sent = %lf", m);
}
*/
/*if(floor(d)==ceil(d) && gcd(d, etf) == 1 && ed % etf == 1)
{
int iterations=0;
if(iterations>e)
{
cout<<"After "<<iterations<<" iterations the value d can't be integer. Moving on . . . "<<endl;
break;
}
//d_data[100] = d;
//cout<<"d_data "<<d_data[100]<<" "<<endl;
cout<<"Iterations counter: "<<iterations<<endl;
break;
}
else
{
k += etf;
iterations++;
}
*/
//cout<<"e "<<e_data[i+1]<<" "<<endl;
//cout<<"d "<<d<<" "<<endl;