-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.cpp
More file actions
274 lines (250 loc) · 8.13 KB
/
Copy pathrsa.cpp
File metadata and controls
274 lines (250 loc) · 8.13 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
// CSCI 411 Personal Project
// April 7th, 2021
// Code by Duncan Hendrickson
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <vector>
#include <string.h>
using namespace std;
/**********************STRUCTS**********************/
/****************************************************
* Struct to store private exponent d and modulus n *
****************************************************/
struct PrivateKey
{
// Modulus
int n;
// Private Exponent
int d;
};
/****************************************************
* Struct to store public exponent e and modulus n *
****************************************************/
struct PublicKey
{
// Modulus
int n;
// Public Exponent
int e;
};
/********************************************************
* Struct holding one public key and one private key *
********************************************************/
struct Keypair
{
PublicKey pub;
PrivateKey pri;
};
/**********************FUNCTIONS**********************/
/****************************************************
* A function to check if given a number is prime *
* Returns a bool *
* p - int - number to check primality *
***************************************************/
bool isPrime(int p)
{
bool prime = true;
if (p <= 0 || p == 1)
{
prime = false;
}
else
{
for (int i = 2; i < p / 2; i++)
{
if (p % i == 0)
{
prime = false;
break;
}
}
}
return prime;
}
/************************************************************************************
* A function that finds the modular inverse via the Extended Euclidian Algorithm *
* a - int - the number you are finding the modular inverse of *
* m - int - the modulus that you are using to find the inverse *
***********************************************************************************/
int modInverse(int a, int m)
{
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
{
// q is quotient
int q = a / m;
int t = m;
// m is remainder now, process same as Euclid's algorithm
m = a % m;
a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
/************************************************************************************
* A function to efficiently calculate a number to a power, then calculate the mod *
* Represented by equation result = base^exponent % modulus *
* Don't have to calculate full value of base^exponent because of the fact that *
* a=bc mod n=(b mod n)⋅(c mod n)mod n *
* Returns a final result int *
* base - int - the number that will be raised to some power *
* exponent - int - the power that the base will be raised to *
* modulus - int - the number that the result will be % by *
***********************************************************************************/
int powerMod(int base, int exponent, int modulus)
{
if (modulus == 1)
return 0;
int result = 1;
base = base % modulus;
while (exponent > 0)
{
if (exponent % 2 == 1) //odd number
result = (result * base) % modulus;
exponent = exponent >> 1; //divide by 2
base = (base * base) % modulus;
}
return result;
}
/************************************************************************************
* Function to generate private and public keys based on user input *
* keys - Keypair - passed by reference to allow modification *
* Takes in user input for primes p and q, calculates n and phi, asks for input for *
* the public exponent then calculates private exponent and stores in keypair *
***********************************************************************************/
void generateKeys(Keypair &keys)
{
int p = 0, q = 0; // Primes
int n; // p*q
int phi; // (p-1)*(q-1)
int e, d; // public and private exponent
cout << "Enter two distinct prime numbers: ";
while (true)
{
// read in user input for p and q
cin >> p;
cin.ignore();
cin >> q;
cin.ignore();
if (p == q)
{
cout << "Those are the same number, try again: " << endl;
}
else if (p < 3 || q < 3)
{
cout << "At least one of those is less than 3, try again: " << endl;
cout << p << " " << q << endl;
}
// ensures they are prime, calculates n and phi
else if (isPrime(p) && isPrime(q))
{
n = p * q;
phi = ((p - 1) * (q - 1));
cout << "n = " << n << endl
<< "phi = " << phi << endl;
break;
}
else
{
cout << "One of those is not prime, try again: " << endl;
}
}
// Get user input for public exponent
cout << "Choose an e that does not share any factors with phi (Try more primes): ";
while (true)
{
cin >> e;
if (!(__algo_gcd(e, phi) == 1))
{
cout << "That doesn't work, try again please: " << endl;
}
// Calculate d using modular inversion
else
{
cout << "Success, Calculating d..." << endl;
d = modInverse(e, phi);
cout << "d = " << d << endl;
break;
}
}
// Store Private and Public Key info
keys.pri.d = d;
keys.pri.n = n;
keys.pub.e = e;
keys.pub.n = n;
cout << "Public Key = (n, e) = (" << n << ", " << e << ")" << endl;
cout << "Private Key = (n, d) = (" << n << ", " << d << ")" << endl;
}
/************************************************************************
* Function to take in user input and encrypt using c = m^e % n *
* pub - PublicKey - stores n and e for this keypair for encryption *
* returns vector of ints that represents each encrypted letter *
***********************************************************************/
vector<int> encrypt(PublicKey pub)
{
// Read in text to encrypt
cout << "Please enter your text to be encrypted: ";
string text;
cin.ignore();
getline(cin, text);
int len = text.length();
vector<int> cypher(len);
// For each character, encrypt using c = m^e % n
for (int i = 0; i < len; i++)
{
int ascii = static_cast<int>(text[i]);
cypher[i] = powerMod(ascii, pub.e, pub.n);
}
// Print encrypted data for aesthetic's sake
cout << endl
<< "Encrypted message in hex:" << endl;
for (int i = 0; i < len; i++)
{
cout << hex << cypher[i];
}
cout << endl
<< endl;
// Return encrypted data
return cypher;
}
/************************************************************************
* Function to take in ciphertext input and decrypt using m = c^d % n *
* pri - PrivateKey - stores n and d for this keypair for decryption *
* cypher - vector<int> - represents each encrypted letter *
***********************************************************************/
void decrypt(PrivateKey pri, vector<int> cypher)
{
int decrypt[cypher.size()];
cout << "Your message decrypted: " << endl;
// For each character, decrypt using m = c^d % n
for (int i = 0; i < cypher.size(); i++)
{
decrypt[i] = powerMod(cypher[i], pri.d, pri.n);
cout << static_cast<char>(decrypt[i]);
}
cout << endl;
}
/**********************MAIN**********************/
int main()
{
// Create an empty keypair
Keypair keys;
// Generate keys with user input
generateKeys(keys);
// Encrypt user inputed text with public key
vector<int> secret = encrypt(keys.pub);
// Immediately decrypt
decrypt(keys.pri, secret);
return 0;
}