-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptingMaths.cpp
More file actions
358 lines (290 loc) · 6.33 KB
/
EncryptingMaths.cpp
File metadata and controls
358 lines (290 loc) · 6.33 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <iostream>
#include"Accounts.h"
#pragma once
using namespace std;
//All mathematical. Leave it
//////////////////////////////////////////////////////
//Calculates the absolute value
int absolute(float x)
{
int y;
y = x;
return y;
}
//Calcultes gcd of two arguments passed
int euclidean_algorithm(int a, int b)
{
int r;
do
{
r = a % b;
a = b;
b = r;
} while (b != 0);
return a;
}
//Calcultes modular inverse
int modular_inverse(int a, int m)
{
float y = 1;
while (1)
{
if ((1 + m * y) / a == absolute((1 + m * y) / a))
break;
else
y++;
}
return (1 + m * y) / a;
}
//Calculates totient of two primes passed as argument
int totient(int a, int b)
{
return (a - 1)*(b - 1);
}
//Modular exponentian.
long long int modular_exponentiation(int b, int e, int m)
{
int counter = 1;
long long int c = b;
while (counter<e) {
c *= b;
c = c % m;
counter++;
}
return c;
}
int key(int e, int m)
{
return modular_inverse(e, m);
}
//////////////////////////////////////////////////////
//Counts number of lines in file data.data
//Used to initialize array in object of type database
int line_count()
{
int number_of_lines = 0;
string line;
ifstream myfile("data.dat");
while (getline(myfile, line))
++number_of_lines;
return number_of_lines;
}
///////////////////////////////////
///////ACCOUNTS DEFINITIONS////////
///////////////////////////////////
void Account::save()
{
ofstream outfile;
outfile.open("data.dat", std::ios_base::app);
//sets cursor at the end of file
outfile.seekp(0, ios::end);
outfile << Encrypt(password) << " "
<< Encrypt(games_played) << " "
<< Encrypt(wins) << " "
<< Encrypt(stalemates) << " "
<< Encrypt(email) << "\n";
outfile.close();
}
//Number encryptor
int Account::Encrypt(int m)
{
return modular_exponentiation(m, e_exponent, n_modulus);
}
//Number decryptor
int Account::Decrypt(int m)
{
return modular_exponentiation(m, d_exponent, n_modulus);
}
//String encryptor
string Account::Encrypt(string input)
{
int n = input.length();
char* c = new char[n + 1];
c[n] = '\0';
for (int i = 0; i<n; i++)
{
c[i] = input.at(i) + i;
}
string output = c;
return output;
}
//String decryptor
string Account::Decrypt(string input)
{
int n = input.length();
char* c = new char[n + 1];
c[n] = '\0';
for (int i = 0; i<n; i++)
{
c[i] = input.at(i) - i;
}
string output = c;
return output;
}
/////////////////////////////////////
///////// DATABASE /////////////////
////////////////////////////////////
Database::Database() : n(line_count())
{
//Creates an array of appropriate size
accounts = new Account[n];
//Reads data in the array
read();
}
Database::~Database()
{
//Saves data before destroying database object
save();
}
//Number encryptor
int Database::Encrypt(int m)
{
return modular_exponentiation(m, e_exponent, n_modulus);
}
//Number decryptor
int Database::Decrypt(int m)
{
return modular_exponentiation(m, d_exponent, n_modulus);
}
//String encryptor
string Database::Encrypt(string input)
{
int n = input.length();
char* c = new char[n + 1];
c[n] = '\0';
for (int i = 0; i<n; i++)
{
c[i] = input.at(i) + i;
}
string output = c;
return output;
}
//String decryptor
string Database::Decrypt(string input)
{
int n = input.length();
char* c = new char[n + 1];
c[n] = '\0';
for (int i = 0; i<n; i++)
{
c[i] = input.at(i) - i;
}
string output = c;
return output;
}
//GETTERS
/////////////////////////////////////
int Database::get_number_of_accounts()
{
return n;
}
string Database::get_email(int i)
{
return accounts[i].email;
}
string Database::get_password(int i)
{
return accounts[i].password;
}
int Database::get_games_played(int i)
{
return accounts[i].games_played;
}
int Database::get_wins(int i)
{
return accounts[i].wins;
}
int Database::get_stalemates(int i)
{
return accounts[i].stalemates;
}
/////////////////////////////////////
//Reads data from file into database object after and decrypts
void Database::read()
{
ifstream infile;
infile.open("data.dat");
cout << "input";
for (int i = 0; i<n; ++i)
{
infile >> accounts[i].password
>> accounts[i].games_played
>> accounts[i].wins
>> accounts[i].stalemates
>> accounts[i].email;
accounts[i].email = Decrypt(accounts[i].email);
accounts[i].password = Decrypt(accounts[i].password);
accounts[i].games_played = Decrypt(accounts[i].games_played);
accounts[i].wins = Decrypt(accounts[i].wins);
accounts[i].stalemates = Decrypt(accounts[i].stalemates);
infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
infile.close();
}
//Searches for a particular email
//If found returns position in array, else returns -1
int Database::email_search(string email)
{
for (int i = 0; i<n; i++)
{
if (email == accounts[i].email)
return i;
}
return -1;
}
//Checks password of user at particular index
//Returns index of user if correct else returns -1
bool Database::password_check(int n, string password)
{
if (password == accounts[n].password)
return true;
else
return false;
}
//Saves data onto the file data.dat in encrypted form
void Database::save()
{
ofstream outfile;
outfile.open("data.dat");
for (int i = 0; i<n; i++)
{
outfile << Encrypt(accounts[i].password) << " "
<< Encrypt(accounts[i].games_played) << " "
<< Encrypt(accounts[i].wins) << " "
<< Encrypt(accounts[i].stalemates) << " "
<< Encrypt(accounts[i].email) << "\n";
}
outfile.close();
}
//Creates a new account in database object and saves it in file
void Database::new_account(string ml, string psswrd)
{
Account* a;
a = new Account;
a->email = ml;
a->password = psswrd;
a->wins = 0;
a->stalemates = 0;
a->games_played = 0;
a->save();
delete a;
accounts = new Account[++n];
read();
}
//INCREMENTORS
/////////////////////////////////////
void Database::increment_wins(unsigned int i)
{
accounts[i].wins++;
save();
}
void Database::increment_stalemates(unsigned int i)
{
accounts[i].stalemates++;
save();
}
void Database::increment_games_played(unsigned int i)
{
accounts[i].games_played++;
save();
}