-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path059.cpp
executable file
·63 lines (57 loc) · 1.3 KB
/
059.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#define FILENAME "cipher1.txt"
#define FIRST_LETTER 'a'
#define LAST_LETTER 'z'
#define NCHARS 3
#define SEPARATOR ','
#define FIRST_KEY "aaa"
#define KEYWORD "Gospel"
using namespace std;
bool nextKey(string &key) {
for (int i = NCHARS - 1; i >= 0; i--) {
if (key[i] != LAST_LETTER) {
key[i] += 1;
for (int j = NCHARS - 1; j > i; j--) key[j] = FIRST_LETTER;
return true;
}
}
return false;
}
string decrypt(vector<int> ciphered, string key) {
int i = 0;
string pl = "";
for (vector<int>::iterator it = ciphered.begin(); it != ciphered.end();
it++) {
pl += *it ^ key[i];
if (++i == NCHARS) i = 0;
}
return pl;
}
int main() {
ifstream file;
file.open(FILENAME);
string oneN;
vector<int> ciphered;
while (getline(file, oneN, SEPARATOR)) {
stringstream tempSs(oneN);
int nAsInt;
tempSs >> nAsInt;
ciphered.push_back(nAsInt);
}
file.close();
string key = FIRST_KEY;
do {
string plain = decrypt(ciphered, key);
if (plain.find(KEYWORD) != string::npos) {
int sum = 0;
int length = plain.length();
for (int i = 0; i < length; i++) sum += plain[i];
cout << sum << endl;
return 0;
}
} while(nextKey(key));
return 0;
}