-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenc.cpp
More file actions
237 lines (229 loc) · 7.09 KB
/
enc.cpp
File metadata and controls
237 lines (229 loc) · 7.09 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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main (int argc, char * argv[]){
if (argc > 1 && strncmp(argv[1], "-h", 2) == 0){
cout << "Encrypt a file using XOR encryption\n\n";
cout << "proper syntax : " << argv[0] << " -h -d -v -k=[key] -f=[h|c|d] -o=[filename] [data]\n\n";
cout << " OPTIONS DESCRIPTION\n ------- -----------\n";
cout << " -h : Display help\n\n";
cout << " -d : Decrypt instead of encrypt. format modifiers are applied\n";
cout << " to input instead of output\n\n";
cout << " -v : Be verbose with output\n\n";
cout << " -k=[key] : Only mandatory option, unless -h is specified\n";
cout << " [key] specifies the key to be used for Encrypion\n\n";
cout << " -f=[h|c|d] : Specify the format to output the encrypted data\n";
cout << " h == Hex output\n";
cout << " c == Character output\n";
cout << " d == Decimal output\n\n";
cout << " -o=[filename] : Output to file [filename] instead of standard output\n\n";
cout << " [data] : This is the data to Encrypt, therefore it is mandatory.\n";
cout << " Every string that is passed to enc will be encrypted.\n";
cout << " Strings are seperated by blank spaces so remember to\n";
cout << " put data containing spaces inside quotation marks (\"\").\n";
cout << " Data can be piped into enc, but it is necessary to use\n";
cout << " xargs. long strings with spaces should be null terminated,\n";
cout << " and the -0 or --null xargs option should be used.\n";
cout << " EXAMPLES\n --------\n\n";
cout << " enc -k=secret_key -f=d -o=EncryptedFile.txt \"Hello world!\"\n\n";
cout << " echo -e \"Hello world!\\0\" | xargs -0 enc -k=secret_key\n";
}
else if (argc < 3){
cout << "proper syntax : " << argv[0] << " -h -d -v -k=[key] -f=[h|c|d] -o=[filename] [data]" << endl;
}
else{
uint8_t args = 6;
bool kflg = false;
bool fflg = false;
bool oflg = false;
bool vflg = false;
bool dflg = false;
uint8_t keylen;
char key[32];
char format;
uint8_t fnlen;
char fname[32];
uint8_t dstrt = 1;
ofstream file;
if (argc < 7){
args = argc - 1;
}
for (int i = 1; i < args; i++){
if (strncmp(argv[i], "-k=", 3) == 0){
keylen = strlen(argv[i]) - 3;
strncpy(key, argv[i] + 3, keylen);
key[keylen] = '\0';
kflg = true;
dstrt++;
if (vflg == true){
cout << "Encrypting data with key \"" << key << "\"\n";
}
}
else if (strncmp(argv[i], "-f=", 3) == 0){
format = argv[i][3];
fflg = true;
dstrt++;
if (vflg == true){
cout << "Using format modifier \"" << format << "\"\n";
switch (format){
case 'd':
cout << "Data will be output in decimal format\n";
break;
case 'c':
cout << "Data will be output in character format\n";
break;
case 'h':
cout << "Data will be output in hexadecimal format\n";
break;
}
}
}
else if (strncmp(argv[i], "-o=", 3) == 0){
fnlen = strlen(argv[i]) - 3;
strncpy(fname, argv[i] + 3, fnlen);
fname[fnlen] = '\0';
oflg = true;
dstrt++;
if (vflg == true){
cout << "File name for data output \"" << fname << "\"\n";
}
}
else if (strncmp(argv[i], "-v", 2) == 0){
vflg = true;
dstrt++;
}
else if (strncmp(argv[i], "-d", 2) == 0){
dflg = true;
dstrt++;
}
}
if (kflg != true){
cout << "-k=[key] parameter required" << endl;
}
else if (dflg == true){
if (oflg == true){
file.open(string(fname));
}
int tmpn;
char tmp;
for (int i = dstrt; i < argc; i++){
if (fflg == true){
switch (format){
case 'd':
tmpn = (int)strtoul(argv[i], NULL, 0);
tmp = tmpn ^ key[(i - dstrt) % keylen];
if (oflg == true){
file << tmp;
}
else{
cout << tmp;
}
break;
case 'c':
tmp = argv[i][0] ^ key[(i - dstrt) % keylen];
if (oflg == true){
file << tmp;
}
else{
cout << tmp;
}
break;
case 'h':
tmpn = (int)strtoul(argv[i], NULL, 16);
tmp = tmpn ^ key[(i - dstrt) % keylen];
if (oflg == true){
file << tmp;
}
else{
cout << tmp;
}
break;
}
}
else {
tmpn = (int)strtoul(argv[i], NULL, 16);
tmp = tmpn ^ key[(i - dstrt) % keylen];
if (oflg == true){
file << tmp;
}
else{
cout << tmp;
}
}
}
if (oflg == true){
file.close();
}
}
else {
size_t count = 0;
if (oflg == true){
file.open(string(fname));
}
for (int i = dstrt; i < argc; i++){
for (int o = 0; o < strlen(argv[i]); o++){
char tmp = argv[i][o];
int tmpn = tmp ^ key[count % keylen];
if (fflg == true){
switch (format){
case 'c':
if (oflg == true){
file << (char)tmpn;
file << ' ';
}
else{
cout << (char)tmpn;
cout << ' ';
}
break;
case 'd':
if (oflg == true){
file << tmpn;
file << ' ';
}
else {
cout << tmpn;
cout << ' ';
}
break;
case 'h':
if (oflg == true){
file << setfill('0') << setw(2) << hex << tmpn;
file << ' ';
}
else {
cout << setfill('0') << setw(2) << hex << tmpn;
cout << ' ';
}
break;
default :
cout << "Wrong format specified in -f option" << endl;
break;
}
}
else{
if (vflg == true){
cout << "Data will be output in hexadecimal format\n";
}
if (oflg == true){
file << setfill('0') << setw(2) << hex << tmpn;
cout << ' ';
}
else {
cout << setfill('0') << setw(2) << hex << tmpn;
cout << ' ';
}
}
count++;
}
}
cout << endl;
if (oflg == true){
file.close();
}
}
}
return 0;
}