-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
460 lines (391 loc) · 10.6 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
/*==============================================
The Willow encryption algorithm
By: Galinski Benjamin
==============================================*/
// Stable v1.0.1
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <cstdint>
#include <iomanip>
#include "mainConfig.hpp"
#include "nlohmann/json.hpp"
#include "manual.hpp"
//===============STRUCTS=======================
//struct for holding the command line arguments
struct inputInfo
{
static uint8_t pathToFollow;
// static uint16_t level;
static char fill;
static char replace;
static std::string fileTarget;
static std::string graphicTarget;
static std::string keyTarget;
static std::string outTarget;
static void dump()
{
std::cout << "pathToFollow: " << (int)inputInfo::pathToFollow << std::endl;
// std::cout << "level: " << (int)inputInfo::level << std::endl;
std::cout << "fill: " << inputInfo::fill << std::endl;
std::cout << "replace: " << (int)inputInfo::replace << std::endl;
std::cout << "fileTarget: " << inputInfo::fileTarget << std::endl;
std::cout << "graphicTarget: " << inputInfo::graphicTarget << std::endl;
std::cout << "keyTarget: " << inputInfo::keyTarget << std::endl;
std::cout << "outTarget: " << inputInfo::outTarget << std::endl;
}
};
uint8_t inputInfo::pathToFollow = 0;
//uint16_t inputInfo::level = 5;
char inputInfo::fill = '@';
char inputInfo::replace = (char)0;
std::string inputInfo::fileTarget = "";
std::string inputInfo::graphicTarget = "";
std::string inputInfo::keyTarget = "Breadfish";
std::string inputInfo::outTarget = "./out";
//struct for holding json content as a stream
struct jData
{
static uint64_t count;
static std::stringstream ss;
static uint64_t get_countMod()
{
return jData::count*2;
}
static void build(std::string &jsonFile)
{
//Create flat json
std::ifstream f_target(jsonFile, std::ios::in);
nlohmann::json jData;
f_target >> jData;
f_target.close();
jData = jData.flatten();
//Put flat json data into stream and get count
std::stringstream ss_jData_temp;
char i;
ss_jData_temp << jData;
while(ss_jData_temp.get(i))
{
jData::count++;
jData::ss << i;
}
}
};
uint64_t jData::count = 0;
std::stringstream jData::ss;
//struct for holding the ascii art as a stream
struct graphic
{
static uint64_t count;
static std::stringstream ss;
static void build(std::string &graphicFile, char &fill)
{
std::ifstream f_graphic(graphicFile, std::ios::in);
char i;
while(f_graphic.get(i))
{
if(i == fill) graphic::count++;
graphic::ss << i;
}
}
};
uint64_t graphic::count = 0;
std::stringstream graphic::ss;
//======================PROTOTYPES============================
bool encrypt(void);
bool decrypt(void);
bool count(void);
bool willowEncrypt(void);
bool varify(void);
bool monochrome(void);
bool fileExists(std::string file);
//=====================FUNCTIONS==============================
//interprests user input and builds inputInfo
int main(int argc, char *argv[])
{
std::string testVal;
for(uint8_t i = 1; i < argc; i++)
{
testVal = argv[i];
//============OPERATIONS====================
if(testVal == "-e" || testVal == "--encryption")
{
inputInfo::pathToFollow = 1;
if(fileExists(argv[++i])) break;
inputInfo::fileTarget = argv[i];
if(fileExists(argv[++i])) break;
inputInfo::graphicTarget = argv[i];
inputInfo::keyTarget = argv[++i];
}
else if(testVal == "-d" || testVal == "--decrypt")
{
inputInfo::pathToFollow = 2;
if(fileExists(argv[++i])) break;
inputInfo::fileTarget = argv[i];
inputInfo::keyTarget = argv[++i];
}
else if(testVal == "-c" || testVal == "--count")
{
inputInfo::pathToFollow = 3;
if(fileExists(argv[++i])) break;
std::string temp = argv[i];
if(temp.substr(temp.size() - 5) == ".json")
{
inputInfo::fileTarget = temp;
}
else
{
inputInfo::graphicTarget = temp;
}
if(i+1 < argc)
{
if(fileExists(argv[++i])) break;
temp = argv[i];
if(temp.substr(temp.size() - 5) == ".json")
{
inputInfo::fileTarget = temp;
}
else
{
inputInfo::graphicTarget = temp;
}
}
}
else if(testVal == "-m" || testVal == "--monochrome")
{
inputInfo::pathToFollow = 4;
inputInfo::graphicTarget = argv[++i];
if(fileExists(argv[++i])) break;
inputInfo::fileTarget = argv[i];
}
//==============FLAGS=======================
else if(testVal == "-f" || testVal == "--fill")
{
inputInfo::fill = argv[++i][0];
}
else if(testVal == "-o" || testVal == "--out")
{
inputInfo::outTarget = argv[++i];
}
else if(testVal == "-r" || testVal == "--replace")
{
inputInfo::replace = argv[++i][0];
}
//============OTHER=========================
else if(testVal == "-h" || testVal == "--help")
{
print_man();
return 1;
}
else if(testVal == "-t" || testVal == "--tellmemore!")
{
tellThemMore();
return 1;
}
else
{
youDunGoofed();
return 1;
}
}
//where to go?
if(inputInfo::pathToFollow == 1) //--encrypt
{
if(encrypt()) return 1;
}
else if(inputInfo::pathToFollow == 2) //--decrypt
{
if(decrypt()) return 1;
}
else if(inputInfo::pathToFollow == 3) //--count
{
if(count()) return 1;
}
else if(inputInfo::pathToFollow == 4) //--monochrome
{
if(monochrome()) return 1;
}
else if(inputInfo::pathToFollow == 255) // File not found
{
noFileExists();
return 1;
}
return 0;
}
//==============================================================
bool encrypt(void)
{
jData::build(inputInfo::fileTarget);
graphic::build(inputInfo::graphicTarget, inputInfo::fill);
if(varify()) return 1;
if(willowEncrypt()) return 1;
return 0;
}
//=============================================
bool decrypt(void)
{
std::ifstream f_data(inputInfo::fileTarget, std::ios::in);
std::string temp = "";
bool odd = false;
uint64_t iter = 0;
char i;
char x;
uint16_t brac_count = 0;
while(f_data.get(i))
{
//if a hex number "0123456789" (48-57), "abcdef" (97-102)
if(((int)i >= 48 && (int)i <= 57) || ((int)i >= 97 && (int)i <= 102))
{
temp += i;
//appends 2 chars then translates them
if(odd)
{
x = (char)((inputInfo::keyTarget[(iter++ % inputInfo::keyTarget.size())]) ^ (std::stoi(temp, nullptr, 16)));
if(x == '{') brac_count++;
else if(x == '}') brac_count--;
//if the first bracket of the json has met its closing bracket (end of json data)
if(brac_count == 0)
{
jData::ss << x;
f_data.close();
nlohmann::json jsonFile;
jData::ss >> jsonFile;
jsonFile = jsonFile.unflatten();
std::ofstream f_out(inputInfo::outTarget, std::ios::out);
f_out << jsonFile.dump();
f_out.close();
return 0;
}
jData::ss << x;
temp.clear();
}
odd = !odd;
}
}
f_data.close();
return 0;
}
//==============================================================
bool monochrome(void)
{
std::ifstream f_graphicOld(inputInfo::fileTarget, std::ios::in);
std::ofstream f_graphic(inputInfo::outTarget, std::ios::out);
char i;
while(f_graphicOld.get(i))
{
//if the char you want to NOT change
if(i == inputInfo::graphicTarget[0] || i == '\n')
{
if(i == '\n') f_graphic << i;
else if(inputInfo::replace != (char)0) f_graphic << inputInfo::replace;
else f_graphic << i;
}
//else replace with your fill char
else f_graphic << inputInfo::fill;
}
f_graphic.close();
f_graphicOld.close();
return 0;
}
//================================================================
bool count(void)
{
if((inputInfo::fileTarget != "") && (inputInfo::graphicTarget != ""))
{
jData::build(inputInfo::fileTarget);
graphic::build(inputInfo::graphicTarget, inputInfo::fill);
if(varify()) return 1;
std::cout << "json data count is: " << jData::get_countMod() << std::endl;
std::cout << "Template has: " << graphic::count << " " << inputInfo::fill <<"'s" << std::endl;
std::cout << "You're good to go!" << std::endl;
}
else if(inputInfo::fileTarget != "")
{
jData::build(inputInfo::fileTarget);
std::cout << "json data count is: " << jData::get_countMod() << std::endl;
return 0;
}
else if(inputInfo::graphicTarget != "")
{
graphic::build(inputInfo::graphicTarget, inputInfo::fill);
std::cout << "Template has: " << graphic::count << " " << inputInfo::fill << "'s" << std::endl;
return 0;
}
return 0;
}
//==============================================================
bool willowEncrypt(void)
{
std::ofstream f_out(inputInfo::outTarget, std::ios::out);
std::stringstream out_temp;
char charData;
char charGraphic;
char temp;
uint64_t iter = 0;
uint64_t jDataCount = jData::count;
if(!f_out.is_open()) return 1;
//for every char in the flat json
while(jData::ss.get(charData))
{
//XOR the first char of the key with the first char of json, second with second...
temp = inputInfo::keyTarget[(iter++ % inputInfo::keyTarget.size())];
out_temp << std::setfill('0') << std::setw(2) << std::hex << (int)(charData^temp);
}
//until the encrypted stream is the same size as the number of fill chars in the ascii art
while((jDataCount+2) < graphic::count)
{
//fill empty space with random nonsense
temp = inputInfo::keyTarget[(iter++ % inputInfo::keyTarget.size())];
out_temp << std::setfill('0') << std::setw(2) << std::hex << (((uint8_t)std::rand())^temp);
jDataCount += 2;
}
//if the number of fill chars in the art is odd, it will need the one extra random nonsense
while(jDataCount < graphic::count)
{
out_temp << std::hex << (((uint8_t)std::rand())%16);
jDataCount++;
}
//begin to map the encrypted data to the ascii art
while(graphic::ss.get(charGraphic))
{
if(charGraphic == inputInfo::fill)
{
out_temp.get(charData);
f_out << charData;
}
else
{
f_out << charGraphic;
}
}
f_out.close();
return 0;
}
//===========================================
bool varify(void)
{
long long int diff = (jData::get_countMod() - graphic::count);
if(diff > 0)
{
std::cout << "ERROR!" << std::endl;
std::cout << "The ascii template has " << graphic::count << " " << inputInfo::fill << "'s" << std::endl;
std::cout << "The json file is " << jData::get_countMod() << " long (char length of the flattened json contents times 2)" << std::endl;
std::cout << "Your ascii template is " << diff << " characters too small" << std::endl;
std::cout << "You can use '-c' to check the sizes of these files" << std::endl;
return 1;
}
return 0;
}
//===========================================
bool fileExists(std::string file)
{
std::ifstream f(file, std::ios::in);
if(!f.is_open())
{
inputInfo::pathToFollow = 255;
return 1;
}
f.close();
return 0;
}