-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
232 lines (202 loc) · 5.49 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
#include "Headers/includes.h"
// 1
void aesExample() {
string actual_string;
AES::aes_init(AES::OPTIONS::doGenerateKey); // Call before use
// Output key
cout << "Key: ";
for (int i = 0; i < sizeof(AES::KEY::key) / sizeof(AES::KEY::key[0]); ++i) {
cout << AES::KEY::key[i] << " ";
};
cout << endl;
cout << "Input what you would like encrypted:" << endl;
cin.ignore();
getline(cin >> noskipws, actual_string);
cout << endl
<< "Plaintext to be encrypted:" << endl
<< actual_string << endl;
// Encryption, output ciphertext
actual_string = AES::encrypt(actual_string);
cout << "Encrypted ciphertext:" << endl << actual_string << endl;
// Decrypt, output plaintext
actual_string = AES::decrypt(actual_string);
cout << "Decrypted plaintext:" << endl << actual_string << endl;
cout << "Press any key to continue" << endl;
cin.ignore();
return;
};
// 2
void textFromFileExample() {
string input;
cout << "Please input the path to the file: " << endl;
cin.ignore();
cin >> input;
if (!AES::encryptFF(input)) {
return;
}; // returns false if path is bad
ifstream readfile(input);
string line;
cout << "Encrypted: " << endl;
while (getline(readfile, line)) {
cout << line << endl;
};
readfile.close();
if (!AES::decryptFF(input)) {
return;
};
readfile.open(input);
cout << "Decrypted: " << endl;
while (getline(readfile, line)) {
cout << line << endl;
};
readfile.close();
};
// 3
inline void wholeFileEncryptionExample() {
AES::aes_init(AES::OPTIONS::doGenerateKey);
cout << "Generated key: ";
for (int i = 0; i < sizeof(AES::KEY::key) / sizeof(AES::KEY::key[0]); ++i) {
cout << AES::KEY::key[i] << " ";
};
cout << endl << endl << endl;
string pass;
printf("Please input a password\n");
cin.ignore();
cin >> pass;
string input;
printf(
"Please input the path to the file (please see testFiles folder): \n");
cin.ignore();
cin >> input;
if (!AES::encryptFile(input, pass)) {
return;
}; // returns false if path is bad
printf("Please input new path to .aesenc file\n");
cin.ignore();
string newInput;
cin >> newInput;
printf("Please input your password\n");
cin.ignore();
cin >> pass;
printf("Encrypted\nDecrypting...\n");
thread dec(
AES::decryptFile,
ref(newInput),
ref(pass),
"",
(AES::FILE_FLAGS)(
AES::FILE_FLAGS::deleteAesencFile |
AES::FILE_FLAGS::deleteKeyFile));
dec.join();
};
// 5
void aes_debug() {
cout << "AES SECTION" << endl;
cout << "Testing file functions" << endl << endl;
cout << "1mb" << endl;
cout << "Reading" << endl;
ifstream file("testFiles/1mb/1mb.txt");
file.seekg(0, file.end);
int length = file.tellg();
file.seekg(0, file.beg);
char *buffer = new char[length];
file.read(buffer, length);
string text = buffer;
delete[] buffer;
file.close();
cout << "Testing" << endl;
cout << "Init" << endl;
AES::aes_init(AES::OPTIONS::doGenerateKey);
cout << "Encrypting" << endl;
AES::encryptFile("testFiles/1mb/1mb.txt", "test");
cout << "Decrypting" << endl;
AES::decryptFile(
"testFiles/1mb/1mb.aesenc",
"test",
"",
(AES::FILE_FLAGS)(
AES::FILE_FLAGS::deleteAesencFile |
AES::FILE_FLAGS::deleteKeyFile));
cout << "Comparing" << endl;
file.open("testFiles/1mb/1mb.txt");
file.seekg(0, file.end);
length = file.tellg();
file.seekg(0, file.beg);
buffer = new char[length];
file.read(buffer, length);
string text2 = buffer;
delete[] buffer;
file.close();
if (text == text2) {
cout << "Test 1 Success" << endl;
} else {
for (long int i = 0; i < text.length(); i++) {
cout << text[i] << " | " << text2[i] << " | " << (text[i] == text2[i]) << endl;
if (text[i] != text2[i]) {break;};
};
cout << "Test 1 Fail" << endl;
};
cout << "Testing Standard Encrypt & Decrypt" << endl;
text = "this is a test";
text2 = AES::encrypt(text);
text2 = AES::decrypt(text2);
if (text == text2) {
cout << "Test 2 Success" << endl;
} else {
cout << "Test 2 Fail" << endl;
};
cout << "Testing bitmap, check image to verify test" << endl;
AES::encryptFile("testFiles/bitmap/img.bmp", "test");
AES::decryptFile("testFiles/bitmap/img.aesenc", "test", "", (AES::FILE_FLAGS)(AES::FILE_FLAGS::deleteAesencFile | AES::FILE_FLAGS::deleteKeyFile));
cout << "finished" << endl;
};
string text[6] = {
"AES = 1",
"AES text from file = 2",
"AES whole file encryption = 3",
"List all files = 4",
"AES Debug = 5",
"Please input the cooresponding number to your desired example"};
bool firstRun = false;
int main() {
if (firstRun == false) {
cout << "Thank you for using my Encryption/Decryption header" << endl
<< "Please visit my Github at ArthurF23" << endl
<< VERSION_INFO::GITLINK << endl << "Version " << VERSION::ver << endl;
cout << "Revised " << VERSION_INFO::REVISION_DATE << endl;
firstRun = true;
};
cout << "\n##########################\n"
<< "\nWhich example would you like to use?" << endl;
cout << text[0] << endl;
cout << text[1] << endl;
cout << text[2] << endl;
cout << text[3] << endl;
cout << text[4] << endl;
cout << text[5] << endl;
char input;
cin >> input;
string files, folders;
switch (input) {
case '1':
aesExample();
break;
case '2':
textFromFileExample();
break;
case '3':
wholeFileEncryptionExample();
break;
case '4':
FileOP::searchDir((char *)"testFiles/", files, folders, true);
cout << " ------ " << endl << files << endl << " ------ " << endl;
break;
case '5':
aes_debug();
break;
default:
cout << "Err please input 1, 2, 3, 4, 5, 6, or 7" << endl;
return main();
};
return main();
};