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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
| #include "void.h"
const string alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя ;.,?!\"'><{}[]-";
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <iomanip>
using namespace std;
string read_from_file(string filename) {
string content = "";
ifstream fin;
fin.open(filename.c_str());
if (!fin.is_open()) {
cout << "Ошибка открытия файла для чтения содержимого." << endl;
fin.close();
return "";
}
getline(fin, content);
fin.close();
return content;
}
void write_to_file(string message, string filename) {
ofstream fout;
fout.open(filename.c_str());
if (!fout.is_open()) {
cout << "Ошибка открытия файла для записи." << endl;
fout.close();
return;
}
else {
fout << message;
}
fout.close();
}
string caesar_cipher_text(int offset, string txt) {
int length = alphabet.length();
string message = "";
int index = -1;
for (int i = 0; i < txt.length(); i++) {
index = alphabet.find(txt.at(i));
if (index != -1) {
message += alphabet.at((index + offset) % length);
}
}
return message;
}
string caesar_decipher_text(int offset, string txt) {
int length = alphabet.length();
offset = offset % length;
string message = "";
int index = -1;
for (int i = 0; i < txt.length(); i++) {
index = alphabet.find(txt.at(i));
if (index != -1) {
message += alphabet.at((index - offset + length) % length);
}
}
return message;
}
string getUniqueCodeWord() {
string code_word = "";
bool isUnique;
do {
isUnique = true;
cout << "Пожалуйста, укажите кодовое слово, не содержащее повторяющихся символов.\nОбратите внимание: регистр букв учитывается:" << endl;
cin.ignore();
getline(cin, code_word);
for (int i = 0; i < code_word.length(); i++) {
if (i != code_word.rfind(code_word.at(i))) {
isUnique = false;
break;
}
}
} while (!isUnique);
return code_word;
}
string code_word_cipher_text(string code_word, string txt) {
string modified_alphabet = alphabet;
for (int i = 0; i < code_word.length(); i++) {
modified_alphabet.erase(modified_alphabet.find(code_word.at(i)), 1);
}
modified_alphabet = code_word + modified_alphabet;
string message = "";
int index = -1;
for (int i = 0; i < txt.length(); i++) {
index = alphabet.find(txt.at(i));
if (index != -1)
message += modified_alphabet.at(index);
}
return message;
}
string code_word_decipher_text(string code_word, string txt) {
string modified_alphabet = alphabet;
for (int i = 0; i < code_word.length(); i++) {
modified_alphabet.erase(modified_alphabet.find(code_word.at(i)), 1);
}
modified_alphabet = code_word + modified_alphabet;
string message = "";
int index = -1;
for (int i = 0; i < txt.length(); i++) {
index = modified_alphabet.find(txt.at(i));
if (index != -1)
message += alphabet.at(index);
}
return message;
}
string fixed_repl_cipher_text(string txt) {
int n = 0;
cout << "Укажите длину последовательности:" << endl;
cin >> n;
cout << "Укажите саму последовательность:" << endl;
int* replacement = new int[n];
for (int i = 0; i < n; i++) {
bool isAppropriate = true;
do {
if (!isAppropriate) {
cout << "Вы указали неподходящее требованиям число. Укажите другое:" << endl;
}
isAppropriate = true;
cin >> replacement[i];
if (replacement[i] > n || replacement[i] < 1) {
isAppropriate = false;
}
for (int j = 0; j < i; j++) {
if (replacement[i] == replacement[j]) {
isAppropriate = false;
break;
}
}
} while (!isAppropriate);
}
string message = "";
for (int i = 0; i < txt.length() % n; i++) {
txt += " ";
}
int cycles = txt.length() / n;
for (int i = 0; i < cycles; i++) {
for (int j = 0; j < n; j++) {
message += txt.at(replacement[j] + i * n - 1);
}
}
return message;
}
string fixed_repl_decipher_text(string txt) {
int n = 0;
cout << "Укажите длину последовательности:" << endl;
cin >> n;
cout << "Укажите саму последовательность:" << endl;
int* replacement = new int[n];
for (int i = 0; i < n; i++) {
bool isAppropriate = true;
do {
if (!isAppropriate) {
cout << "Вы указали неподходящее требованиям значение. Укажите другое:" << endl;
}
isAppropriate = true;
(cin >> replacement[i]).get();
if (replacement[i] > n || replacement[i] < 1) {
isAppropriate = false;
}
for (int j = 0; j < i; j++) {
if (replacement[i] == replacement[j]) {
isAppropriate = false;
break;
}
}
} while (!isAppropriate);
}
string message = "";
int cycles = txt.length() / n;
unsigned char* temp = new unsigned char[n];
for (int i = 0; i < cycles; i++) {
for (int j = 0; j < n; j++) {
if ((replacement[j] + i * n - 1) < txt.length())
temp[replacement[j] - 1] = txt.at(j + i * n);
}
for (int k = 0; k < n; k++) {
message += temp[k];
temp[k] = '\0';
}
}
delete[] temp;
return message;
}
void menu() {
const int MINPOINT = 1;
const int MAXPOINT = 5;
int action = 0;
do {
do {
cout << "Укажите, что необходимо сделать:\n\
1. Добавить исходный текст и записать его в файл.\n\
2. Зашифровать исходное сообщение.\n\
3. Расшифровать сообщение.\n\
4. Вывести содержимое файла на экран.\n\
5. Выйти.\n\nВыберите пункт: ";
cin >> action;
} while (action < MINPOINT || action > MAXPOINT);
switch (action) {
case 1: {
string line = "";
string filename = "";
cout << "\nВведите текст внизу без символов переноса строки(\\n):" << endl;
cin.ignore();
getline(cin, line);
cout << "Укажите название файла, в котором требуется сохранить исходный текст:" << endl;
getline(cin, filename);
write_to_file(line, filename);
break;
}
case 2: {
string fn_from = "";
string fn_to = "";
cout << "Укажите название файла, в котором сохранён исходный текст:" << endl;
cin.ignore();
getline(cin, fn_from);
cout << "Укажите название файла, в который следует записать шифротекст: " << endl;
getline(cin, fn_to);
cout << "Укажите, какое шифрование (кодирование) использовать:\n\
1. Шифр Цезаря.\n\
2. Шифр с кодовым словом.\n\
3. Шифр с зафиксированной перестановкой.\n\
4. Назад." << endl;
int choice = 0;
while (choice < 1 || choice > 4) {
cin >> choice;
}
switch (choice) {
case 1: {
int offset = 0;
do {
cout << "Укажите положительный сдвиг:" << endl;
(cin >> offset).get();
} while (offset < 0);
string cipher = caesar_cipher_text(offset, read_from_file(fn_from));
write_to_file(cipher, fn_to);
break;
}
case 2: {
string cipher = code_word_cipher_text(getUniqueCodeWord(), read_from_file(fn_from));
write_to_file(cipher, fn_to);
break;
}
case 3: {
string cipher = fixed_repl_cipher_text(read_from_file(fn_from));
write_to_file(cipher, fn_to);
break;
}
default: {
cout << "Возвращаюсь назад...";
break;
}
}
break;
}
case 3: {
string fn_from = "";
string fn_to = "";
cout << "Укажите название файла, в котором сохранён шифротекст:" << endl;
cin.ignore();
getline(cin, fn_from);
cout << "Укажите название файла, в который следует записать расшифрованный текст:" << endl;
getline(cin, fn_to);
cout << "Укажите, какое шифрование (кодирование) использовать:\n\
1. Шифр Цезаря.\n\
2. Шифр с кодовым словом\n\
3. Шифр с зафиксированной перестановкой.\n\
4. Назад." << endl;
int choice = 0;
while (choice < 1 || choice > 4) {
cin >> choice;
}
switch (choice) {
case 1: {
int offset = 0;
do {
cout << "Укажите положительный сдвиг:" << endl;
(cin >> offset).get();
} while (offset < 0);
string plaintxt = caesar_decipher_text(offset, read_from_file(fn_from));
write_to_file(plaintxt, fn_to);
break;
}
case 2: {
string plaintxt = code_word_decipher_text(getUniqueCodeWord(), read_from_file(fn_from));
write_to_file(plaintxt, fn_to);
break;
}
case 3: {
string plaintxt = fixed_repl_decipher_text(read_from_file(fn_from));
write_to_file(plaintxt, fn_to);
break;
}
default: {
cout << "Возвращаюсь назад...";
break;
}
}
break;
}
case 4: {
string fn_from = "";
cout << "Укажите название файла, который следует прочитать:" << endl;
cin.ignore();
getline(cin, fn_from);
string content = read_from_file(fn_from);
cout << content << endl;
break;
}
default:
cout << "Выход..." << endl;
break;
}
} while (action != MAXPOINT);
} |