-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.cpp
More file actions
296 lines (268 loc) · 7.14 KB
/
crypt.cpp
File metadata and controls
296 lines (268 loc) · 7.14 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "crypt.h"
void WordsInfo::setParametres(char *pointer, int len, int encr) {
word = (char*)malloc(len * sizeof(char) + 1);
for (int i = 0; i < len; ++i)
word[i] = *pointer++;
word[len] = '\0';
length = len;
encrypted = encr;
}
int WordsInfo::getLength(void) {
return length;
}
int WordsInfo::getEncrypted(void) {
return encrypted;
}
char* WordsInfo::getWord(void) {
return word;
}
Cryptogram::Cryptogram() {
FILE *file;
int i = 0;
char symbol;
file = fopen("cryptogram.txt", "r");
if (file == NULL) {
printf("cryptogram.txt opening error\n");
system("pause");
exit(-1);
}
fseek(file, 0, SEEK_END);
cryptogramSize = ftell(file);
cryptogram = (char*)malloc(cryptogramSize + 1);
fseek(file, 0, SEEK_SET);
while (fscanf(file, "%c", &symbol) != EOF)
{
cryptogram[i] = symbol;
++i;
}
cryptogramSize = i;
cryptogram[i - 1] = '\0';
fclose(file);
}
char* Cryptogram::getCryptogram(void) {
return cryptogram;
}
int Cryptogram::getCryptogramSize(void) {
return cryptogramSize;
}
void CryptoanalyseInstrument::showCryptogram(void) {
printf("%s", myCryptogram.getCryptogram());
}
void Cryptogram::saveCryptogram(void) {
FILE *ready;
ready = fopen("ready.txt", "w");
for (int i = 0; i < cryptogramSize; i++)
fprintf(ready, "%c", cryptogram[i]);
fclose(ready);
}
CryptoanalyseInstrument::CryptoanalyseInstrument() {
maxLength = maxEncrypted = 0;
int i, j, temp_alph;
float temp = 0.0, count = 0.0;
char *tmp_crypt = myCryptogram.getCryptogram();
for (j = 0; j < myCryptogram.getCryptogramSize(); ++j)
{
for (i = 0; i < ALPH_SIZE; i++)
if (distributionAphabet[i] == tmp_crypt[j])
{
frequency[i] += 1.0;
count++;
continue;
}
}
for (i = 0; i < ALPH_SIZE; ++i)
{
frequency[i] = frequency[i] / count;
}
for (i = 1; i < ALPH_SIZE; i++)
{
for (j = 0; j < ALPH_SIZE - i; j++)
{
if (frequency[j] < frequency[j + 1]) {
temp = frequency[j];
frequency[j] = frequency[j + 1];
frequency[j + 1] = temp;
temp_alph = distributionAphabet[j];
distributionAphabet[j] = distributionAphabet[j + 1];
distributionAphabet[j + 1] = temp_alph;
}
}
}
mainCicle();
}
void CryptoanalyseInstrument::wordsAnalysis(void) {
char *letters = NULL;
int length = 0, encrypted = 0, flag = 0;
char *tmp_crypt = myCryptogram.getCryptogram();
if (!wordsInfoList.empty()) deleteWordsInfo();
for (int i = 0; i < myCryptogram.getCryptogramSize(); ++i)
{
if ((tmp_crypt[i] >= 'À' && tmp_crypt[i] <= 'ß') || (tmp_crypt[i] >= 'à' && tmp_crypt[i] <= 'ÿ') || (tmp_crypt[i] >= 'a' && tmp_crypt[i] <= 'z'))
{
if (flag == 0) {
letters = &tmp_crypt[i];
flag = 1;
}
length++;
for (int j = 0; j < 33; j++)
{
if (tmp_crypt[i] == alphabet[j]) { encrypted++; continue; }
}
}
else
{
if (length != 0) addNewWordsInfo(letters, length, encrypted);
if (length > maxLength) maxLength = length;
if (encrypted > maxEncrypted) maxEncrypted = encrypted;
length = 0;
encrypted = 0;
flag = 0;
letters = NULL;
}
}
}
void CryptoanalyseInstrument::addNewWordsInfo(char *letters, int length, int encrypted) {
WordsInfo newWI;
newWI.setParametres(letters, length, encrypted);
wordsInfoList.push_back(newWI);
}
void CryptoanalyseInstrument::deleteWordsInfo(void) {
wordsInfoList.clear();
}
void CryptoanalyseInstrument::showWordsLength(void) {
maxLength = 0;
wordsAnalysis();
for (int i = maxLength; i > 0; i--) {
for (list<WordsInfo>::iterator iter = wordsInfoList.begin();
iter != wordsInfoList.end();
++iter) {
if (iter->getLength() == i)
printf("%d: %s\n", iter->getLength(), iter->getWord());
}
}
}
void CryptoanalyseInstrument::showWordsEncrypted(void) {
maxEncrypted = 0;
wordsAnalysis();
for (int i = maxEncrypted; i > 0; i--) {
for (list<WordsInfo>::iterator iter = wordsInfoList.begin();
iter != wordsInfoList.end();
++iter) {
if (iter->getEncrypted() == i)
printf("%d: %s\n", iter->getEncrypted(), iter->getWord());
}
}
}
void CryptoanalyseInstrument::showAnalysisResult(void) {
for (int i = 0; i < ALPH_SIZE; ++i)
{
printf("%c - %f - %c\n", distributionAphabet[i], frequency[i], rusLettersDistribution[i]);
}
}
void CryptoanalyseInstrument::doReplacement(void) {
char *tmp_crypt = myCryptogram.getCryptogram();
char from, to;
printf("Replace from ");
cin >> from;
printf("to ");
cin >> to;
addNewReplacement(from, to);
for (int i = 0; i < myCryptogram.getCryptogramSize(); i++) {
if (tmp_crypt[i] == from) tmp_crypt[i] = to;
}
}
void CryptoanalyseInstrument::addNewReplacement(char letfirst, char letsecond) {
Replacement newRep;
newRep.setParametres(letfirst, letsecond);
replacementList.push_back(newRep);
}
void Replacement::setParametres(char from, char to) {
letterFrom = from;
letterTo = to;
}
char Replacement::getLetFrom(void) {
return letterFrom;
}
char Replacement::getLetTo(void) {
return letterTo;
}
void CryptoanalyseInstrument::showReplacementHistory(void) {
if (replacementList.empty()) {
cout << "Replacement's history is empty" << endl;
return;
}
for (list<Replacement>::iterator iter = replacementList.begin();
iter != replacementList.end(); ++iter)
cout << iter->getLetFrom() << " changed to " << iter->getLetTo() << endl;
}
void CryptoanalyseInstrument::doRollbackFromLastReplacement(void) {
if (replacementList.empty()) {
cout << "Rollback is impossible - no replacements!" << endl;
return;
}
char from, to;
//list<Replacement>::iterator lastReplacement = replacementList.end();
Replacement lastReplacement = replacementList.back();
from = lastReplacement.getLetTo();
to = lastReplacement.getLetFrom();
replacementList.pop_back();
char *tmp_crypt = myCryptogram.getCryptogram();
for (int i = 0; i < myCryptogram.getCryptogramSize(); i++) {
if (tmp_crypt[i] == from) tmp_crypt[i] = to;
}
}
void CryptoanalyseInstrument::doAutoReplacement(void) {
char *tmp_crypt = myCryptogram.getCryptogram();
for (int i = 0; i < myCryptogram.getCryptogramSize(); i++)
for (int j = 0; j < ALPH_SIZE; j++)
if (tmp_crypt[i] == distributionAphabet[j])
tmp_crypt[i] = rusLettersDistribution[j];
}
int CryptoanalyseInstrument::menu(void) {
int n = 0;
printf("\nChoose your action: \n"
"1. Show result of frequency analysis.\n"
"2. Show current cryptogram.\n"
"3. Show list of words grouped by the letters count.\n"
"4. Show list of words grouped by the encrypted letters count.\n"
"5. Do letters replacement in cryptogram.\n"
"6. Show replacements history.\n"
"7. Do rollback from last replacement.\n"
"8. Do frequency autoreplacement.\n"
"9. Save cryptogram.\n"
"0. Exit.\n"
"Your action >> ");
scanf("%d", &n);
return n;
}
void CryptoanalyseInstrument::mainCicle(void) {
bool working = true;
int button = menu();
while (working)
{
switch (button)
{
case 1: showAnalysisResult();
break;
case 2: showCryptogram();
break;
case 3: showWordsLength();
break;
case 4: showWordsEncrypted();
break;
case 5: doReplacement();
break;
case 6: showReplacementHistory();
break;
case 7: doRollbackFromLastReplacement();
break;
case 8: doAutoReplacement();
break;
case 9: myCryptogram.saveCryptogram();
break;
case 0: working = false;
break;
}
if (working == true) button = menu();
}
}