-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrsa.cpp
132 lines (100 loc) · 2.13 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
#include "rsa.h"
RSA::RSA(){
srand (static_cast <unsigned> (time(0)));
generate_keys();
}
int RSA::get_private_key(){
return this->d;
}
std::vector<int> RSA::get_public_keys(){
std::vector<int> vec;
vec.push_back(this->n);
vec.push_back(this->e);
return vec;
}
std::vector<int> RSA::chy(std::string string){
std::vector<int> ret;
for(char letter : string){
int ascii = ord(letter);
int crip = pow(ascii, this->e);
crip = mod(crip, this->n);
ret.push_back(crip);
std::cout << letter<<" "<<crip << std::endl;
}
return ret;
}
std::string RSA::unchy(std::string){
}
void RSA::generate_keys(){
this->p = generate_prime();
this->q = generate_prime();
this->n = p*q;
int y = totient(p);
int x = totient(q);
this->toti_n = x * y;
this->e = generate_e();
this->d = generate_private_key();
}
int RSA::totient(int n){
if(isprime(n)){
return n - 1;
}
}
bool RSA::isprime(int n){
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n%2 == 0 || n%3 == 0) return false;
for (int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
}
int RSA::generate_e(){
while(true){
int generated = this->generate_random(2, toti_n);
if(this->mdc(this->toti_n, generated) == 1){
return generated;
}
}
}
int RSA::mdc(int x, int y){
int rest = 1;
while(y != 0){
rest = x%y;
x = y;
y = rest;
}
return x;
}
int RSA::ord(char c){
return (int) c;
}
char RSA::chr(int i){
return (char) i;
}
int RSA::generate_prime(){
int e;
do{
e = generate_random();
}while(!isprime(e));
return e;
}
int RSA::generate_random(int from, int to){
return from + (rand()%to);
}
int RSA::mod(int x, int y){
if(x < y){
return x;
}
return x%y;
}
int RSA::generate_private_key(){
int d = 0;
while(mod(d*this->e, this->toti_n) != 1){
d++;
}
return d;
}