-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.cpp
78 lines (72 loc) · 2.26 KB
/
stream.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctype.h>
using namespace std;
int main () {
string line;
vector<vector<char>> cyphers;
ifstream myfile ("cyphers.txt");
int max_len = 0;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
int cur_len = line.length();
vector<char> cur_line_chars;
for (int i = 0; i < line.length(); i+=2)
{
char curr_char = stoul (line.substr (i,2), nullptr, 16);
cur_line_chars.push_back(curr_char);
}
if (cur_line_chars.size() > max_len)
{
max_len = cur_line_chars.size();
}
cyphers.push_back(cur_line_chars);
}
myfile.close();
}
else cout << "Unable to open file";
vector<char> key;
for (int i = 0; i < max_len; i++) // for every character in the stream
{
char best_key_guess;
int max_successful_letters = 0;
for (int j = 0; j < cyphers.size(); j++) // for every message
{
int successful_letters = 0;
char cypher_char = cyphers[j][i];
for (int k = 0; k < cyphers.size(); k++) // for every message (that has enough letters)
{
if (cyphers[k].size() > i)
{
char xored_char = cypher_char ^ cyphers[k][i];
if (toupper(xored_char) >= 'A' && toupper(xored_char) <= 'Z')
{
successful_letters++;
}
if (successful_letters > max_successful_letters)
{
max_successful_letters = successful_letters;
best_key_guess = cypher_char ^ ' ';
}
}
}
}
key.push_back(best_key_guess);
}
cout << "Broken code:\n";
for (int i = 0; i < cyphers.size(); i++)
{
cout << i << '|';
for (int j = 0; j < cyphers[i].size(); j++)
{
char text_char = cyphers[i][j] ^ key[j];
cout << text_char;
}
cout << "|\n";
}
return 0;
}