-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriplesFrequency.cpp
113 lines (96 loc) · 2.42 KB
/
triplesFrequency.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
/*
This program is for cracking a simple substitution cipher with frequency analysis
Krypton Level 2 ? Level 3 (http://overthewire.org/wargames/krypton/krypton3.html)
Plan
1) Compare every three letter string with all other three letter strings
2) Frequency analysis of three letter strings
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
class tripFreq {
public:
string trip;
int freq;
tripFreq(string _trip, int _freq) {
trip = _trip;
freq = _freq;
}
};
bool largerTrips(tripFreq* t1, tripFreq* t2) {
return (t1->freq < t2->freq);
}
int main() {
vector <tripFreq*> allTrips;
tripFreq* trip;
ofstream myfile;
myfile.open ("analysisTrips.txt");
ifstream file("ciphertxtTrips.txt");
//store the ciphertext as a string
string temp;
string fullCipherTxt;
while(getline(file, temp)) {
fullCipherTxt += temp;
}
int count = 0;
int checkNew = 0;
for (int b = 0; b < fullCipherTxt.length() - 2; b++) {
checkNew = 0;
count = 0;
string test(1, fullCipherTxt[b]);
test += fullCipherTxt[b+1];
test += fullCipherTxt[b+2];
for (int i = 0; i < allTrips.size(); i++) {
if (allTrips.at(i)->trip == test) {
checkNew = 1; //Stop processing if the triple already exists in the vector
}
}
if (checkNew == 0) {
for (int a = 0; a < fullCipherTxt.length() - 2; a++) {
string triplet(1, fullCipherTxt[a]);
triplet += fullCipherTxt[a+1];
triplet += fullCipherTxt[a+2];
//count the occurences of the triple
if (test==triplet) {
count++;
}
}
trip = new tripFreq(test, count);
allTrips.push_back(trip);
}
//doing nothing if the triple was already in the vector
}
sort(allTrips.begin(), allTrips.end(), largerTrips); //sort vector from highest to lowest occurences
for (int i = 0; i < allTrips.size(); i++) {
myfile << "trip: " << allTrips.at(i)->trip << ", count: " << allTrips.at(i)->freq <<endl;
}
cin >> temp;
return 0;
}
/* Some output
trip: DQF, count: 12
trip: BXJ, count: 13
trip: UYS, count: 13
trip: SWQ, count: 13
trip: SWC, count: 13
trip: YSQ, count: 13
trip: SUQ, count: 13
trip: QNS, count: 13
trip: UDQ, count: 14
trip: JCB, count: 14
trip: SUY, count: 14
trip: JDQ, count: 15
trip: CBG, count: 15
trip: CGE, count: 16
trip: JSN, count: 16
trip: SNS, count: 19
trip: DCU, count: 19
trip: DSN, count: 22
trip: SQN, count: 23
trip: QGW, count: 27
trip: JDS, count: 61
*/