/
challenge4.cpp
260 lines (228 loc) · 4.06 KB
/
challenge4.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
//Detect Single Byte XOR
//Answer: Now that the party is jumping
#include <iostream>
#include <string.h>
#include <map>
#include <sstream>
#include <math.h>
#include <vector>
#include <fstream>
using namespace std;
//Hashmap contains hex key and binary values
map<char, string> gen_table()
{
map<char, string> map;
map['0'] = "0000";
map['1'] = "0001";
map['2'] = "0010";
map['3'] = "0011";
map['4'] = "0100";
map['5'] = "0101";
map['6'] = "0110";
map['7'] = "0111";
map['8'] = "1000";
map['9'] = "1001";
map['a'] = "1010";
map['b'] = "1011";
map['c'] = "1100";
map['d'] = "1101";
map['e'] = "1110";
map['f'] = "1111";
return map;
}
string con_hex_2_bin(string hexStr)
{
map<char,string> m = gen_table();
string newStr = "";
for(int i=0; i<hexStr.size(); i++)
{
if(isdigit(hexStr[i]))
{
newStr += m.find(hexStr[i])->second;
}
else
{
newStr += m.find(hexStr[i])->second;
}
// newStr += m.find(hexStr[i])->second;
}
return newStr;
}
vector<int> con_bin_2_dec(string str)
{
vector<int> v;
string newStr = "";
istringstream iss(str);
string x;
while(iss >> x)
{
double power = 7.0;
double decimal = 0.0;
for(int i=0; i<x.size(); i++)
{
if(x[i] == '1')
{
decimal += pow(2.0,power);
}
power--;
}
v.push_back((int)decimal);
}
return v;
}
string add_spaces(string str)
{
string newStr = "";
for(int i=0; i<str.size(); i+=8)
{
newStr += str[i];
newStr += str[i+1];
newStr += str[i+2];
newStr += str[i+3];
newStr += str[i+4];
newStr += str[i+5];
newStr += str[i+6];
newStr += str[i+7];
newStr += " ";
}
return newStr;
}
//Return the char frequency of the string
map<char, int> frequencyTable(string str)
{
map<char, int> m;
map<char, int>::iterator it;
for(int i=0; i<str.size(); i++)
{
char ch = str[i];
it = m.find(ch);
if(it == m.end())
{
m.insert(make_pair(ch, 1));
}
else
{
it->second++;
}
}
return m;
}
//dec to ascii
string con_dec_2_ascii(vector<int> v)
{
string newStr = "";
for(int i=0; i<v.size(); i++)
{
newStr += (char) v[i];
}
return newStr;
}
//Map Frequency
map<char, int> getFrequency(string str)
{
map<char, int> m;
map<char, int>::iterator it;
for(int i=0; i<str.size(); i++)
{
char ch = str[i];
it = m.find(ch);
if(it == m.end())
{
m.insert(make_pair(ch,1));
}
else
{
it->second++;
}
}
return m;
}
//Print the hashmap
void print_map(map<char,int> m)
{
// int count = 0;
cout << "{";
for(auto p: m)
{
if(isalpha(p.first))
{
cout << p.first << ":" << p.second << " ";
// count += p.second;
}
}
cout << "}" << endl;
}
//High frequency for strings with more alphabets
int high_frequency(map<char,int> m)
{
int count = 0;
for(auto p: m)
{
if(isalpha(p.first))
{
// cout << p.first << ":" << p.second << " ";
count += p.second;
}
}
return count;
}
void mainFunction(string str)
{
str = add_spaces(con_hex_2_bin(str));
vector<int> v1 = con_bin_2_dec(str);
//cout << con_bin_2_dec(v1) << endl;
// unsigned char a = 88; //because 'X' has the largest score
// for(int j=0; j<v1.size(); j++)
// {
// unsigned char b = v1[j];
// unsigned char c = b ^ a;
// cout << c << "";
// }
// cout << endl;
// std::copy(v1.begin(), v1.end(), std::ostream_iterator<int>(std::cout, " "));
}
int main()
{
ifstream infile;
string str;
int maxCount = 0;
string maxString = "";
vector<int> maxV;
infile.open("enctext.txt");
if(!infile)
{
cout << "Unable to open file" << endl;
exit(1);
}
while(infile >> str)
{
if(str.size() == 60)
{
str = add_spaces(con_hex_2_bin(str));
vector<int> v1 = con_bin_2_dec(str);
string newStr = con_dec_2_ascii(v1);
int count = high_frequency(getFrequency(newStr));
if(count > maxCount)
{
maxCount = count;
maxString = newStr;
maxV = v1;
}
}
}
cout << maxString << " " << maxCount << endl;
for(int i=0; i<256; i++)
{
unsigned char a = i;
cout << "CHAR " << a <<" : ";
for(int j=0; j<maxV.size(); j++)
{
unsigned char b = maxV[j];
unsigned char c = b ^ a;
cout << c << "";
}
cout<<endl;
}
cout << endl;
return 0;
}