-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.cpp
More file actions
364 lines (333 loc) · 12.2 KB
/
Copy pathUserInterface.cpp
File metadata and controls
364 lines (333 loc) · 12.2 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
359
360
361
362
363
364
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include "PasswordEntry.h"
#include "UserInterface.h"
using namespace std;
auto UserInterface::UserInterface::showMenuInfo() {
cout << "Menu: " << endl;
cout << "1. Search passwords." << endl;
cout << "2. Sort passwords." << endl;
cout << "3. Add password." << endl;
cout << "4. Edit password." << endl;
cout << "5. Delete password." << endl;
cout << "6. Add category." << endl;
cout << "7. Remove category." << endl;
cout << "8. Save file." << endl;
cout << "9. Display passwords." << endl;
cout << "10. Show menu." << endl;
cout << "11. Exit program." << endl;
}
void UserInterface::UserInterface::runMainMenu() {
bool exit_loop = false;
string choice;
int choice_int;
do {
showMenuInfo();
cout << "Please enter a number: ";
cin >> choice;
cout << endl;
try {
choice_int = stoi(choice);
} catch (const invalid_argument argument) {
choice_int = 0;
}
switch (choice_int) {
case 1:
cout << "1. Search passwords." << endl;
cout << endl;
searchPasswords();
break;
case 2:
cout << "2. Sort passwords." << endl;
cout << endl;
sortPasswords();
break;
case 3: {
cout << "3. Add password." << endl;
PasswordEntry entry = *new PasswordEntry();
if (entry.createNewPassword(categories)) {
password_entries.push_back(entry);
}
break;
}
case 4:
cout << "4. Edit password." << endl;
cout << endl;
editPassword();
break;
case 5:
cout << "5. Delete password." << endl;
cout << endl;
deletePasswords();
break;
case 6:
cout << "6. Add category." << endl;
addCategory();
break;
case 7:
cout << "7. Remove category." << endl;
removeCategory();
break;
case 8:
cout << "8. Save file." << endl;
saveFile();
break;
case 9:
cout << "9. Display passwords." << endl;
cout << endl;
displayPasswords();
break;
case 10:
showMenuInfo();
cout << endl;
break;
case 11:
cout << "Exiting program." << endl;
cout << endl;
exit_loop = true;
break;
default:
cout << "Invalid choice. Please enter a valid option." << endl;
cout << endl;
break;
}
} while (!exit_loop);
}
void UserInterface::UserInterface::displayPasswords() {
for (PasswordEntry entry : password_entries) {
cout << entry.toDisplayString() << endl;
}
}
void UserInterface::UserInterface::addCategory() {
cout << "Please enter the category name to add:" << endl;
cin.ignore();
string category_name;
std::getline(cin, category_name);
categories.push_back(category_name);
}
void UserInterface::UserInterface::removeCategory() {
cout << "Please enter the category name to remove:" << endl;
cin.ignore();
string category_name;
std::getline(cin, category_name);
vector<string>::iterator new_categories_end;
new_categories_end = remove(categories.begin(), categories.end(), category_name);
categories.erase(new_categories_end, categories.end());
cout << "Categories after removal: " << endl;
for (string cat : categories) {
cout << cat << endl;
}
auto new_entries_end = remove_if(password_entries.begin(), password_entries.end(),
[category_name](PasswordEntry entry_to_remove) {
if (category_name == entry_to_remove.getCategory()) {
cout << "Removing password: " + entry_to_remove.toStorageString()
<< endl;
return true;
}
return false;
});
if (new_entries_end != password_entries.end()) {
password_entries.erase(new_entries_end, password_entries.end());
}
}
void UserInterface::UserInterface::deletePasswords() {
cout << "Please enter the name of the password to delete:" << endl;
cin.ignore();
string password_name;
std::getline(cin, password_name);
vector<string>::iterator entries_after_removal;
auto new_entries_end = remove_if(password_entries.begin(), password_entries.end(),
[password_name](PasswordEntry entry_to_remove) {
if (password_name == entry_to_remove.getName()) {
cout << "Removing password: " + entry_to_remove.toStorageString()
<< endl;
return true;
}
return false;
});
if (new_entries_end != password_entries.end()) {
password_entries.erase(new_entries_end, password_entries.end());
}
}
void UserInterface::UserInterface::searchPasswords() {
cout << "Please enter the name of the password to search for:" << endl;
cin.ignore();
string search_term;
std::getline(cin, search_term);
for (PasswordEntry entry : password_entries) {
if (search_term == entry.getName()) {
cout << entry.toDisplayString() << endl;
}
}
}
void UserInterface::UserInterface::sortPasswords() {
std::sort(password_entries.begin(), password_entries.end(), &PasswordEntry::compareEntries);
}
void UserInterface::UserInterface::editPassword() {
displayPasswords();
cout << "Enter the name of the password you want to edit: " << endl;
string target_name;
cin.ignore();
std::getline(cin, target_name);
vector<PasswordEntry>::iterator target_iter = std::find_if(password_entries.begin(), password_entries.end(),
[target_name](PasswordEntry const &entry) {
return entry.getName() == target_name;
});
if (target_iter != password_entries.end()) {
target_iter->editEntry(categories);
}
}
bool UserInterface::UserInterface::selectFile() {
std::string folder_path = std::filesystem::current_path();
vector<std::string> file_paths;
cout << "Files in the current application directory: " << endl << endl;
for (const auto &item : std::filesystem::directory_iterator(folder_path)) {
file_paths.push_back(item.path());
std::cout << item.path() << std::endl;
}
cout << "Please enter the file path:" << endl;
getline(cin, file_path);
fstream file_stream(file_path);
if (!file_stream.good()) {
cout << "No file exists at the specified path. A new file will be created." << endl;
file_stream.open(file_path, std::fstream::out);
file_stream.close();
return true;
} else {
return loadFile();
}
return false;
}
void UserInterface::UserInterface::saveFile() {
cout << "Please enter the encryption key: " << endl;
string encryption_key;
cin >> encryption_key;
ofstream output_stream(file_path);
output_stream << embedModificationDate(encrypt(control_text, encryption_key)) << endl;
for (PasswordEntry entry : password_entries) {
output_stream << encrypt(entry.toStorageString(), encryption_key) << endl;
}
output_stream.close();
}
void UserInterface::UserInterface::testEncryption() {
cout << "Please enter the encryption password: " << endl;
string encryption_key;
cin.ignore();
getline(cin, encryption_key);
cout << control_text << endl;
string encrypted_control = encrypt(control_text, encryption_key);
cout << encrypted_control << endl;
cout << decrypt(encrypted_control, encryption_key) << endl;
}
bool UserInterface::UserInterface::loadFile() {
cout << "Please enter the decryption key: " << endl;
string decryption_key;
cin >> decryption_key;
ifstream input_stream(file_path);
string decrypted_line;
string encrypted_line;
getline(input_stream, encrypted_line);
encrypted_line = removeModificationDate(encrypted_line);
decrypted_line = decrypt(encrypted_line, decryption_key);
if (decrypted_line == control_text) {
while (getline(input_stream, encrypted_line)) {
decrypted_line = decrypt(encrypted_line, decryption_key);
PasswordEntry entry;
entry.setFieldsFromStorageLine(decrypted_line);
password_entries.push_back(entry);
categories.push_back(entry.getCategory());
}
return true;
} else {
vector<string> file_contents;
file_contents.push_back(embedModificationDate(encrypted_line));
while (getline(input_stream, encrypted_line)) {
file_contents.push_back(encrypted_line);
}
ofstream output_stream(file_path);
for (string line : file_contents) {
output_stream << line << endl;
}
output_stream.close();
}
return false;
}
string UserInterface::UserInterface::embedModificationDate(string line) {
std::chrono::system_clock::time_point current_time = std::chrono::system_clock::now();
std::time_t time_value = std::chrono::system_clock::to_time_t(current_time);
std::tm* local_time = std::localtime(&time_value);
int year = local_time->tm_year + 1900;
int month = local_time->tm_mon + 1;
int day = local_time->tm_mday;
int hour = local_time->tm_hour;
int minute = local_time->tm_min;
int second = local_time->tm_sec;
line.insert(0, to_string(year));
line.insert(6, zeroPad(month));
line.insert(10, zeroPad(day));
line.insert(14, zeroPad(hour));
line.insert(18, zeroPad(minute));
line.insert(22, zeroPad(second));
return line;
}
string UserInterface::UserInterface::removeModificationDate(string line) {
line.erase(22, 2);
line.erase(18, 2);
line.erase(14, 2);
line.erase(10, 2);
line.erase(6, 2);
line.erase(0, 4);
return line;
}
std::string UserInterface::UserInterface::zeroPad(int time_unit) {
std::ostringstream oss;
oss << std::setw(2) << std::setfill('0') << time_unit;
return oss.str();
}
string UserInterface::UserInterface::encrypt(string plaintext, string key) {
int key_index = 0;
string ciphertext;
for (char plain_char : plaintext) {
if (key_index >= key.size()) {
key_index = 0;
}
char encrypted_char = xorChars(plain_char, key.at(key_index));
ciphertext += to_string(int(encrypted_char)) + ";";
key_index++;
}
return ciphertext;
}
char UserInterface::UserInterface::xorChars(char char1, char char2) {
return char(unsigned(char1 ^ char2));
}
std::string UserInterface::UserInterface::decrypt(std::string ciphertext, std::string key) {
std::stringstream token_stream(ciphertext);
std::string token;
std::string raw_bytes;
while (std::getline(token_stream, token, ';')) {
int ascii_value = stoi(token);
raw_bytes += char(ascii_value);
}
int key_index = 0;
string plaintext;
for (char encrypted_char : raw_bytes) {
if (key_index >= key.size()) {
key_index = 0;
}
char decrypted_char = xorChars(encrypted_char, key.at(key_index));
plaintext += decrypted_char;
key_index++;
}
return plaintext;
}
int main() {
UserInterface::UserInterface ui_manager;
if (ui_manager.selectFile()) {
cout << endl;
ui_manager.runMainMenu();
}
}