-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.cpp
217 lines (169 loc) · 4.07 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
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
using namespace std;
/*
const int p = 3;
const int q = 17;
*/
const int p = 2;
const int q = 13;
int const N = p * q;
int const phi = (p-1)*(q-1);
int e; // encryption key
int d; // decryption key
vector<int> generateFactorsPhi();
void generateCoPrimes( vector<int> & c );
void eliminateFactorsPQ( vector<int> & c );
void eliminateFactorsPhi( vector<int> & c );
void encryptionKey( vector<int> c );
void decryptionKey();
void printVector( vector<int> c );
bool checkChars( string str );
string encryptMessage( string s );
string decryptMessage( string s );
int charToInt( char c );
char intToChar( int n );
int main() {
vector<int> c;
// generate coprime
generateCoPrimes(c);
// generate encryption key
encryptionKey(c);
//generate decryption key
decryptionKey();
char op;
string msg;
cout<<"Enter e to encrypt a message or d to decrypt a message: ";
cin>>op;
if( op == 'e' ){
cout<<"Enter a message to encrypt (A-Z):"<<endl;
cin.ignore();
getline(cin, msg);
if( checkChars(msg) ){
string encrypted = encryptMessage(msg);
cout<<"Encryption: "<<encrypted<<endl;
} else{
cout<<"String contains at least one invalid character."<<endl;
}
} else if( op == 'd' ){
// generate decryption key
cout<<"Enter a message to decrypt:"<<endl;
cin.ignore();
getline(cin, msg);
string decrypted = decryptMessage(msg);
cout<<"Decryption: "<<decrypted<<endl;
}
}
vector<int> generateFactorsPhi(){
vector<int> factorsPhi;
int temp = 1;
while (temp <= phi){
if (not(phi % temp))
factorsPhi.push_back(temp);
temp++;
}
return factorsPhi;
}
void generateCoPrimes( vector<int> & c ){
for( int i = 1; i <= N; i++ ){
c.push_back(i);
}
eliminateFactorsPQ(c);
eliminateFactorsPhi(c);
}
void eliminateFactorsPQ( vector<int> & c ){
for( int i = 0; i < c.size(); i++ ){
if( c[i]%p == 0 || c[i]%q == 0 ){
c.erase(c.begin()+i);
i--;
}
}
}
void eliminateFactorsPhi( vector<int> & c ){
vector<int> phi = generateFactorsPhi();
for( int i = 0; i < c.size(); i++ ){
for( int j = 0; j < phi.size(); j++ ){ // loop through phi factors
if( c[i] % phi[j] == 0 ){
c.erase(c.begin()+i);
i--;
}
}
}
}
void encryptionKey( vector<int> c ){
for( int i = 0; i < c.size(); i++ ){
if( c[i] > 1 && c[i] < phi ){
e = c[i];
break;
}
}
}
void decryptionKey(){
for( int i = 1; i <= phi; i++ ){
if( (e * i) % phi == 1 ){
d = i;
break;
}
}
}
void printVector( vector<int> c ){
for( int i = 0; i < c.size(); i++ ){
cout<<c[i]<<" ";
}
cout<<endl;
}
bool checkChars( string str ){
bool ret = true;
for( int i = 0; i < str.length(); i++ ){
if( (str[i] >= 'A' && str[i] <= 'Z') ){
;
} else{
ret = false;
break;
}
}
return ret;
}
string encryptMessage( string s ){
string ret = "";
for( int i = 0; i < s.length(); i++ ){
int num = charToInt(s[i]);
if( num != -1 ){
unsigned long long temp = pow(num, e);
temp = temp % N;
ret += intToChar(temp);
}
}
return ret;
}
string decryptMessage( string s ){
string ret = "";
for( int i = 0; i < s.length(); i++ ){
int num = charToInt(s[i]);
if( num != -1 ){
unsigned long long temp = pow(num, d);
temp = temp % N;
ret += intToChar(temp);
}
}
return ret;
}
int charToInt( char c ){
/*
A - Z => 0 - 25
*/
int num = -1;
if( c >= 'A' && c <= 'Z' ){
num = c - 'A';
}
return num;
}
char intToChar( int n ){
char c = '\0';
if( n >= 0 && n <= 25 ){
c = n + 'A';
}
return c;
}