-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler59.cpp
162 lines (138 loc) · 3.69 KB
/
euler59.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
//DESCRIPTION: Two analyses help us decrypt this message. First, we do a count to determine the frequencies of each ASCII character in each set. Then, we run through the test lowercase letters and see which ones map to expected characters. Of the possible candidates, only the SPACE character makes sense as the most frequent character, so we try the resulting mapping as our first guess, which turns out to be correct.
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
using namespace std;
vector<char> cipher;
int decrypt_print(char k1, char k2, char k3)
{
int sum = 0;
cout << endl;
cout << "Decrypting with key " << k1 << k2 << k3 << ": " << endl;
int c = 1;
char dec;
for (int i = 0; i < cipher.size(); i++)
{
if (c % 3 == 1)
dec = (char) (cipher[i] ^ k1);
else if (c % 3 == 2)
dec = (char) (cipher[i] ^ k2);
else if (c % 3 == 0)
dec = (char) (cipher[i] ^ k3);
c++;
cout << dec;
sum += dec;
}
cout << endl;
return sum;
}
int main()
{
map<char, int> count1, count2, count3;
cout << "Reading file..." << endl;
ifstream file("cipher.txt");
int t;
int c = 2;
char delim;
file >> t;
count1[t]++;
cipher.push_back((char) t);
while (file.good())
{
file >> delim >> t;
if (c % 3 == 1)
count1[t]++;
else if (c % 3 == 2)
count2[t]++;
else
count3[t]++;
c++;
cipher.push_back((char) t);
}
file.close();
//undo duplicated element
cipher.pop_back();
c--;
if (c % 3 == 1)
count1[t]--;
else if (c % 3 == 2)
count2[t]--;
else
count3[t]--;
cout << "Done reading file." << endl << endl;
cout << "Frequency analysis:" << endl << endl;
cout << "First set:" << endl;
for (map<char, int>::iterator i = count1.begin(); i != count1.end(); i++)
{
cout << (int)i->first << " : " << i->second;
if (i->second > 50)
cout << " **";
cout << endl;
}
cout << endl;
cout << "Second set:" << endl;
for (map<char, int>::iterator i = count2.begin(); i != count2.end(); i++)
{
cout << (int)i->first << " : " << i->second;
if (i->second > 50)
cout << " **";
cout << endl;
}
cout << endl;
cout << "Third set:" << endl;
for (map<char, int>::iterator i = count3.begin(); i != count3.end(); i++)
{
cout << (int)i->first << " : " << i->second;
if (i->second > 50)
cout << " **";
cout << endl;
}
cout << endl << endl;
cout << "Possible candidates for first letter:" << endl;
for (char test = 'a'; test <= 'z'; test++)
{
bool flag = true;
for (map<char, int>::iterator i = count1.begin(); i != count1.end(); i++)
{
char dec = i->first ^ test;
if ((dec <= 31) || (dec >= 126))
flag = false;
}
if (flag)
cout << test << "->" << (char) (test ^ 71) << " ";
}
cout << endl;
cout << "Possible candidates for second letter:" << endl;
for (char test = 'a'; test <= 'z'; test++)
{
bool flag = true;
for (map<char, int>::iterator i = count2.begin(); i != count2.end(); i++)
{
char dec = i->first ^ test;
if ((dec <= 31) || (dec >= 126))
flag = false;
}
if (flag)
cout << test << "->" << (char) (test ^ 79) << " ";
}
cout << endl;
cout << "Possible candidates for third letter, and resulting decryption of most frequent character:" << endl;
for (char test = 'a'; test <= 'z'; test++)
{
bool flag = true;
for (map<char, int>::iterator i = count3.begin(); i != count3.end(); i++)
{
char dec = i->first ^ test;
if ((dec <= 31) || (dec >= 126))
flag = false;
}
if (flag)
cout << test << "->" << (char) (test ^ 68) << " ";
}
cout << endl;
int sum = decrypt_print('g', 'o', 'd');
cout << sum;
cin.get();
return 0;
}