-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
694 lines (570 loc) · 21.1 KB
/
main.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
struct CredentialsEntry {
string username;
string password;
};
CredentialsEntry logged_in;
struct UserData {
string id;
string website_name;
string username;
string password;
};
string CaesarCipherEncryption(string plain_text_input) {
string plain_text = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-=!@#$%^&*()_+{}[]|;:'\"\\,.<>/?~`";
int caesar_shift[6] = {0, 2, 3, 6, 24, 31};
int digit_caesar_shift = 1 ;
int index;
string cipher_text_output;
for (int i = 0, n = plain_text_input.size(); i < n; i++) {
for (int j = 0; j < 96; j++) {
if (plain_text_input[i] == plain_text[j]) {
index = caesar_shift[digit_caesar_shift];
if (j == 95) {
cipher_text_output += plain_text[1+index];
}
else {
cipher_text_output += plain_text[(j+index) % 96];
};
digit_caesar_shift++;
if (digit_caesar_shift % 6 == 0) {
digit_caesar_shift = 1;
};
break;
};
};
};
return cipher_text_output;
};
string CaesarCipherDecryption(string cipher_text_input) {
string plain_text = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-=!@#$%^&*()_+{}[]|;:'\"\\,.<>/?~`";
int caesar_shift[6] = {0, 2, 3, 6, 24, 31};
int digit_caesar_shift = 1 ;
int index;
string plain_text_output;
for (int i = 0, n = cipher_text_input.size(); i < n; i++) {
for (int j = 0; j < 96; j++) {
if (cipher_text_input[i] == plain_text[j]) {
index = caesar_shift[digit_caesar_shift];
if (j - index < 0) {
plain_text_output += plain_text[96 + (j-index)];
}
else {
plain_text_output += plain_text[(j-index) % 96];
};
digit_caesar_shift++;
if (digit_caesar_shift % 6 == 0) {
digit_caesar_shift = 1;
};
break;
};
};
};
return plain_text_output;
};
string SubstitutionCipherEncryption(string plain_text_input) {
string plain_text = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-=!@#$%^&*()_+{}[]|;:'\"\\,.<>/?~`";
string cipher_text = " ~!@#$%^&*()_+`1234567890-=qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?";
plain_text_input = CaesarCipherEncryption(plain_text_input);
string cipher_text_output;
for (int i = 0, n = plain_text_input.size(); i < n; i++) {
for (int j = 0; j < 96; j++) {
if (plain_text_input[i] == plain_text[j]) {
cipher_text_output += cipher_text[j];
};
};
};
return cipher_text_output;
};
string SubstitutionCipherDecryption(string cipher_text_input) {
string plain_text = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-=!@#$%^&*()_+{}[]|;:'\"\\,.<>/?~`";
string cipher_text = " ~!@#$%^&*()_+`1234567890-=qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?";
string plain_text_output;
for (int i = 0, n = cipher_text_input.size(); i < n; i++) {
for (int j = 0; j < 96; j++) {
if (cipher_text_input[i] == cipher_text[j]) {
plain_text_output += plain_text[j];
};
};
};
return CaesarCipherDecryption(plain_text_output);
};
// Pseudo RSA Encryption Algorithm
int InverseOfE(int *phi, int *e) {
int table [2][2] =
{
{*phi, *phi},
{*e, 1}
};
int temp00 = 0;
int temp01 = 0;
while (table[1][0] != 1) {
temp00 = table[1][0];
temp01 = table[1][1];
table[1][1] = table[0][1]-((table[0][0]/table[1][0])*table[1][1]);
if (table[1][1] < 0) {
table[1][1] = *phi % table[1][1];
};
table[1][0] = table[0][0]-((table[0][0]/table[1][0])*table[1][0]);
table[0][0] = temp00;
table[0][1] = temp01;
};
return table[1][1];
};
bool IsPrime(int *num) {
for (int i = 2, n = *num/2; i <= n; i++) {
if (*num % i == 0) {
return false;
};
};
return true;
};
int PseudoRsaEncryption(string p_word, string q_word) {
int p = 0, q = 0;
for (int i = 0, text_length = p_word.size(); i < text_length; i++) {
p += int(p_word[i]);
}
for (int i = 0, text_length = q_word.size(); i < text_length; i++) {
q += int(q_word[i]);
}
while(!IsPrime(&p)) {
p++;
};
while(!IsPrime(&q)) {
q++;
};
double n = p*q;
int phi = (p-1)*(q-1);
int e = 3;
double part_ = pow(n, e);
int cipher_text = int(part_) % int(n);
return cipher_text;
};
int SearchProcess(string line, string query) {
for (int i = 0, n = line.size() - query.size() + 1; i < n; i++) {
if(query == line.substr(i, query.size())) {
return 1;
};
};
return 0;
};
int Search(string *username, string *password) {
cout << endl <<
setfill('*') << setw(20) << '*' << " SEARCH ITEM " << setw(20) << '*' << endl << setfill(' ') << endl <<
setw(5) << ' ' << "Search with a minimum of 3 characters" << endl <<
setw(5) << ' ' << "E - Back to Home" << endl << endl;
UserData data_chunk;
ifstream data_text_file_search("data_text_file.txt");
string query;
do {
cout << "-> ";
cin >> query;
if (query == "E") {
return 1;
}
} while (query.size() < 3);
cout << endl;
if(data_text_file_search.good()) {
int counter = 0;
while(!data_text_file_search.eof()) {
getline(data_text_file_search,data_chunk.id);
if (data_chunk.id == to_string(PseudoRsaEncryption(*username, *password)) && !data_text_file_search.eof() ) {
getline(data_text_file_search, data_chunk.website_name);
getline(data_text_file_search, data_chunk.username);
getline(data_text_file_search, data_chunk.password);
if (SearchProcess(SubstitutionCipherDecryption(data_chunk.website_name), query) || SearchProcess(SubstitutionCipherDecryption(data_chunk.username), query)) {
counter++;
cout << "Website name: " << SubstitutionCipherDecryption(data_chunk.website_name) << endl;
cout << "Username: " << SubstitutionCipherDecryption(data_chunk.username) << endl;
cout << "Password: " << SubstitutionCipherDecryption(data_chunk.password) << endl << endl;
}
};
};
data_text_file_search.close();
if (counter == 1) {
cout << counter << " saved login found." << endl;
}
if (counter > 1) {
cout << counter << " saved logins found." << endl;
}
else if (counter == 0) {
cout << "No saved login found." << endl;
};
}
else {
cout << "No item recorded yet" << endl << endl;
};
return 0;
}
int DeleteItemVault(string *username, string *password) {
cout << endl <<
setfill('*') << setw(20) << '*' << " DELETE ITEM " << setw(20) << '*' << endl << setfill(' ') << endl <<
setw(5) << ' ' << "Select item number to delete" << endl <<
setw(5) << ' ' << "E - Back to Home" << endl << endl;
ifstream data_text_file_read("data_text_file.txt");
UserData data_chunk;
int counter = 0;
if(data_text_file_read.good()) {
while(!data_text_file_read.eof()) {
getline(data_text_file_read,data_chunk.id);
if (data_chunk.id == to_string(PseudoRsaEncryption(*username, *password)) && !data_text_file_read.eof()) {
cout << counter << "." << endl;
counter++;
getline(data_text_file_read, data_chunk.website_name);
cout << "Website name: " << SubstitutionCipherDecryption(data_chunk.website_name) << endl;
getline(data_text_file_read, data_chunk.username);
cout << "Username: " << SubstitutionCipherDecryption(data_chunk.username) << endl;
getline(data_text_file_read, data_chunk.password);
cout << "Password: " << SubstitutionCipherDecryption(data_chunk.password) << endl << endl;
};
};
data_text_file_read.close();
};
string index_to_delete_str;
do {
cout << "-> ";
cin >> index_to_delete_str;
cin.clear();
if (index_to_delete_str == "E") {
return 1;
};
} while (cin.fail());
int index_to_delete = stoi(index_to_delete_str);
ifstream data_text_file_read_1("data_text_file.txt");
ofstream data_text_file_write("temp_data_text_file.txt");
bool entry_deleted = false;
if(data_text_file_read_1.good()) {
counter = 0;
while(!data_text_file_read_1.eof()) {
getline(data_text_file_read_1,data_chunk.id);
if (!data_text_file_read_1.eof()) {
getline(data_text_file_read_1, data_chunk.website_name);
getline(data_text_file_read_1, data_chunk.username);
getline(data_text_file_read_1, data_chunk.password);
// Saves usernames and passwords of other users
if (data_chunk.id != to_string(PseudoRsaEncryption(*username, *password))) {
data_text_file_write << data_chunk.id << endl;
data_text_file_write << data_chunk.website_name << endl;
data_text_file_write << data_chunk.username << endl;
data_text_file_write<< data_chunk.password << endl;
}
// Saves usernames and passwords of the user
else if (data_chunk.id == to_string(PseudoRsaEncryption(*username, *password)) && counter != index_to_delete) {
data_text_file_write << data_chunk.id << endl;
data_text_file_write << data_chunk.website_name << endl;
data_text_file_write << data_chunk.username << endl;
data_text_file_write<< data_chunk.password << endl;
counter++;
}
// Deletes the username and password that user wants to delete
else {
entry_deleted = true;
counter++;
};
};
};
data_text_file_read_1.close();
data_text_file_write.close();
remove("data_text_file.txt");
rename("temp_data_text_file.txt", "data_text_file.txt");
};
if (entry_deleted) {
cout << endl << "Item deleted successfully." << endl;
return 0;
}
else {
cout << endl << "Item not found, try again." << endl;
return 1;
};
return 1;
};
int AddItemVault(string *username, string *password) {
cout << endl <<
setfill('*') << setw(20) << '*' << " ADD ITEM " << setw(20) << '*' << endl << setfill(' ') << endl <<
setw(5) << ' ' << "E - Back to Vault" << endl << endl;
UserData add_item_vault;
// add_item_vault.id = ;
cout << "Website name: ";
cin.ignore();
getline(cin, add_item_vault.website_name);
cin.clear();
if (add_item_vault.website_name == "E") {
return 1;
};
cout << "Username: ";
cin >> add_item_vault.username;
cin.clear();
if (add_item_vault.username == "E") {
return 1;
};
cout << "Password: ";
cin >> add_item_vault.password;
cin.clear();
if (add_item_vault.password == "E") {
return 1;
};
ofstream data_text_file("data_text_file.txt", ios_base::app);
// ID
data_text_file << PseudoRsaEncryption(*username, *password) << endl;
data_text_file << SubstitutionCipherEncryption(add_item_vault.website_name) << endl;
data_text_file << SubstitutionCipherEncryption(add_item_vault.username) << endl;
data_text_file << SubstitutionCipherEncryption(add_item_vault.password) << endl;
data_text_file.close();
return 0;
}
int Vault(string *username, string *password) {
start_Vault:
cout << endl <<
setfill('*') << setw(20) << '*' << " VAULT " << setw(20) << '*' << endl << setfill(' ');
ifstream data_text_file("data_text_file.txt");
cout << endl;
if(data_text_file.good()) {
UserData data_chunk;
while(!data_text_file.eof()) {
// Identifier
getline(data_text_file, data_chunk.id);
if (data_text_file.eof()) {
data_text_file.close();
break;
}
getline(data_text_file, data_chunk.website_name);
getline(data_text_file, data_chunk.username);
getline(data_text_file, data_chunk.password);
if (to_string(PseudoRsaEncryption(*username, *password)) == data_chunk.id) {
cout << "Website name: " << SubstitutionCipherDecryption(data_chunk.website_name) << endl;
cout << "Username: " << SubstitutionCipherDecryption(data_chunk.username) << endl;
cout << "Password: " << SubstitutionCipherDecryption(data_chunk.password) << endl << endl;
}
};
}
else {
cout << "No item recorded yet" << endl << endl;
};
cout <<
setw(5) << ' ' << "A - Add item" << endl <<
setw(5) << ' ' << "D - Delete item" << endl <<
setw(5) << ' ' << "E - Back to Home" << endl << endl;
cout << "-> ";
char vault_option;
cin >> vault_option;
cin.clear();
switch (vault_option) {
case 'A':
switch (AddItemVault(username, password)) {
case 1:
goto start_Vault;
break;
case 0:
cout << endl << "Item saved successfully." << endl;
goto start_Vault;
break;
};
break;
case 'D':
switch (DeleteItemVault(username, password)) {
case 1:
goto start_Vault;
break;
case 0:
goto start_Vault;
break;
};
break;
case 'E':
return 1;
default:
cout << setfill('*') << setw(3) << '*' << "Invalid selection. Try again." << setw(3) << '*' << endl << endl << setfill(' ');
goto start_Vault;
}
return 0;
};
void Home(string *username, string *password) {
start_Home:
cout << endl <<
setfill('*') << setw(20) << '*' << " HOME " << setw(20) << '*' << endl << setfill(' ') <<
setw(2) << ' ' << "Press a letter to enter a page." << endl << endl <<
setw(5) << ' ' << "V - Vault Page" << endl <<
setw(5) << ' ' << "S - Search Page" << endl <<
setw(5) << ' ' << "E - Log Out" << endl << endl;
};
// User Interfaces
void MainMenu() {
cout << endl <<
setfill('*') << setw(20) << '*' << " SECURUS PASSWORD MANAGER " << setw(20) << '*' << endl << setfill(' ') <<
setw(2) << ' ' << "Press a letter to enter a page." << endl << endl <<
setw(5) << ' ' << "L - Log In Page" << endl <<
setw(5) << ' ' << "S - Sign Up Page" << endl <<
setw(5) << ' ' << "E - Exit Program" << endl << endl;
};
// LogInMenu returns 1 when user wants to go back to Main Menu
int LogInMenu() {
cout << endl <<
setfill('*') << setw(20) << '*' << " LOG IN MENU " << setw(20) << '*' << endl << setfill(' ') <<
setw(2) << ' ' << "If you haven't signed up then type E to get into main menu." << endl << endl;
CredentialsEntry log_in_entry;
cout << "Username: ";
cin >> log_in_entry.username;
cin.clear();
// Checks if the user wants to go back to Main Menu
if (log_in_entry.username == "E") {
return 1;
};
cout << "Password: ";
cin >> log_in_entry.password;
cin.clear();
// Checks if the user wants to go back to Main Menu
if (log_in_entry.password == "E") {
return 1;
};
int login_status = 0;
// Define and Initialize Sign up txt file to find email and password
ifstream sign_up_file("sign_up.txt");
string sign_up_file_line_log_in;
if(!sign_up_file.good()) {
cout << endl << endl <<
setfill('*') << setw(3) << '*' << "The username or password you entered isn't a valid one." << setw(3) << '*' << endl << endl;
return 1;
}
while(!sign_up_file.eof()) {
getline(sign_up_file, sign_up_file_line_log_in);
if (sign_up_file.eof()) {
break;
};
if (!(sign_up_file_line_log_in == "")) {
if (stoi(sign_up_file_line_log_in) == PseudoRsaEncryption(log_in_entry.username, log_in_entry.password)) {
cout << endl << "Successfully Logged In" << endl;
login_status = 1;
logged_in.username = log_in_entry.username;
logged_in.password = log_in_entry.password;
return 0;
};
};
};
if (login_status == 0) {
cout << endl << endl <<
setfill('*') << setw(3) << '*' << "The username or password you entered isn't a valid one." << setw(3) << '*' << endl << endl;
return 1;
}
return 1;
};
// SignUpMenu returns 1 when user wants to go back to Main Menu
int SignUpMenu() {
start_SignUpMenu:
cout <<
setfill('*') << setw(20) << '*' << " SIGN UP MENU " << setw(20) << '*' << endl << setfill(' ') <<
setw(2) << ' ' << "If you already signed up then type E to get into main menu." << endl << endl <<
setw(5) << ' ' << "No whitespaces between..." << endl <<
setw(5) << ' ' << "eg. lawlawrencee, rencezz, hahaha12" << endl << endl;
CredentialsEntry sign_up_entry;
cout << "Username: ";
cin >> sign_up_entry.username;
cin.clear();
// Checks if the user wants to go back to Main Menu
if (sign_up_entry.username == "E") {
return 1;
};
cout << "Password: ";
cin >> sign_up_entry.password;
cin.clear();
// Checks if the user wants to go back to Main Menu
if (sign_up_entry.password == "E") {
return 1;
};
// Check for conflicts
ofstream check_file("sign_up.txt", ios_base::app);
check_file.close();
ifstream sign_up_file_conflict_check("sign_up.txt");
string sign_up_file_conflict_check_line;
while(!sign_up_file_conflict_check.eof()) {
getline(sign_up_file_conflict_check, sign_up_file_conflict_check_line);
if (sign_up_file_conflict_check.eof()) {
break;
};
if (!(sign_up_file_conflict_check_line == "")) {
if (stoi(sign_up_file_conflict_check_line) == PseudoRsaEncryption(sign_up_entry.username, sign_up_entry.password)) {
cout << endl << "login credentials already exist. Try again." << endl << endl;
goto start_SignUpMenu;
};
};
};
// Define and Initialize Sign Up txt file
ofstream sign_up_file("sign_up.txt", ios_base::app);
// Add Log In Credentials and Encrypt it using RSA
sign_up_file << PseudoRsaEncryption(sign_up_entry.username, sign_up_entry.password) << endl;
sign_up_file.close();
logged_in.username = sign_up_entry.username;
logged_in.password = sign_up_entry.password;
return 0;
};
int main() {
start:
MainMenu();
cout << "-> ";
char menu_option;
cin >> menu_option;
cin.clear();
switch (menu_option) {
case 'L':
cin.clear();
switch (LogInMenu()) {
case 1:
goto start;
break;
case 0:
goto start_Home;
break;
};
break;
case 'S':
cin.clear();
switch (SignUpMenu()) {
case 1:
goto start;
break;
case 0:
goto start_Home;
break;
};
break;
case 'E':
goto exit;
default:
cout << setfill('*') << setw(3) << '*' << "Invalid selection. Try again." << setw(3) << '*' << endl << endl << setfill(' ');
goto start;
};
goto start;
start_Home:
Home(&logged_in.username, &logged_in.username);
cout << "-> ";
char home_option;
cin >> home_option;
cin.clear();
switch (home_option) {
case 'V':
if(Vault(&logged_in.username, &logged_in.password) == 1) {
goto start_Home;
};
break;
start_Search:
case 'S':
if(Search(&logged_in.username, &logged_in.password) == 1) {
goto start_Home;
}
else {
goto start_Search;
}
break;
case 'E':
goto start;
default:
cout << setfill('*') << setw(3) << '*' << "Invalid selection. Try again." << setw(3) << '*' << endl << endl << setfill(' ');
goto start_Home;
};
exit:
return 0;
}