-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.cc
52 lines (43 loc) · 1.28 KB
/
crypto.cc
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main(int argc, char **argv) {
using namespace std;
if (argc < 2) {
cerr << "Missing arguments\ncrypto [key] input\n";
return 1;
}
fstream input;
fstream output;
stringstream sstream;
if (argc < 3) {
input = fstream(argv[1], ios_base::in | ios_base::binary);
output = fstream(argv[1], ios_base::in | ios_base::out | ios_base::binary);
if (!input.is_open() || !output.is_open()) {
cerr << "No such file\n";
return 1;
}
sstream << cin.rdbuf();
} else {
fstream key(argv[1], ios_base::in | ios_base::binary);
input = fstream(argv[2], ios_base::in | ios_base::binary);
output = fstream(argv[2], ios_base::in | ios_base::out | ios_base::binary);
if (!key.is_open() || !input.is_open() || !output.is_open()) {
cerr << "No such file\n";
return 1;
}
sstream << key.rdbuf();
}
char k = 0, b;
int i = 0;
while (input.get(b)) {
if (sstream.str().length() > 0) {
if (i >= sstream.str().length()) i = 0;
k = sstream.str()[i];
i++;
}
b ^= k;
output.put(b);
}
}