-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit2.cpp
More file actions
524 lines (483 loc) · 13.9 KB
/
Copy pathUnit2.cpp
File metadata and controls
524 lines (483 loc) · 13.9 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//---------------------------------------------------------------------------
#include <vcl.h>
#include <System.IOUtils.hpp>
#pragma hdrstop
#include <set>
#include <iostream>
#include <fstream>
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm* MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner) {}
//---------------------------------------------------------------------------
const std::set<int> nums { 3, 6, 9, 11, 12, 15, 18, 21, 22, 24, 27, 30 };
const UnicodeString RUS_UPPER = L"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
const UnicodeString RUS_LOWER = L"абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
int lastPos = 0;
const int MAX_OPTION = 2;
const int MIN_EXT = 5;
const int MIN_LEN = 1;
const int MAX_LEN = 32;
const int MIN_TEXT = 1;
const int MAX_TEXT = 255;
enum TErrorCode
{
CORRECT,
INCORRECT_CHOICE,
NON_NUMERIC,
OUT_OF_RANGE,
FILE_NOT_TXT,
FILE_NOT_EXIST,
FILE_NOT_READABLE,
FILE_NOT_WRITABLE,
FILE_IS_EMPTY,
FILE_NOT_FULL
};
const std::wstring ERR[] = { L"",
L"Error. Incorrect choice. Please try again. ",
L"Error. Non-numeric value. Please try again. ",
L"Error. Out of Range. Please try again. ",
L"Error. File not .txt. Please try again. ",
L"Error. File not Exist. Please try again. ",
L"Error. File not readable. Please try again. ",
L"Error. File not writable. Please try again. ",
L"Error. File is empty. Please try again. ",
L"Error. The file lacks sufficient information . Please try again. "
};
std::wstring __fastcall getExtension(
const std::wstring &str, const int posStart, const int posEnd)
{
int i;
std::wstring extension;
extension = L"";
for (i = posStart; i < posEnd; i++) {
extension += str[i];
}
return extension;
}
TErrorCode __fastcall isFileTxt(const std::wstring &pathToFile)
{
TErrorCode error;
error = CORRECT;
int len;
len = pathToFile.length();
if (len < MIN_EXT || getExtension(pathToFile, len - 4, len) != L".txt") {
error = FILE_NOT_TXT;
}
return error;
}
TErrorCode __fastcall isFileExist(std::wstring &pathToFile)
{
TErrorCode error;
std::ifstream fileName(pathToFile);
error = CORRECT;
if (!fileName) {
error = FILE_NOT_EXIST;
}
return error;
}
TErrorCode __fastcall isFileReadable(const std::wifstream &fileName)
{
TErrorCode error;
error = CORRECT;
if (!fileName.is_open()) {
error = FILE_NOT_READABLE;
}
return error;
}
TErrorCode __fastcall isFileWritable(const std::wofstream &fileName)
{
TErrorCode error;
error = CORRECT;
if (!fileName.is_open()) {
error = FILE_NOT_WRITABLE;
}
return error;
}
void __fastcall TMainForm::N3Click(TObject* Sender)
{
TMainForm::btFileClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::cbMethodChange(TObject* Sender)
{
if (cbMethod->ItemIndex == 1) {
edKey->Text = "";
edKey->NumbersOnly = false;
udKey->Enabled = false;
udKey->Visible = false;
edKey->Width = 145 * 4;
reDestText->Text = "";
lbKeyHint->Visible = false;
} else {
udKey->Enabled = true;
udKey->Visible = true;
edKey->NumbersOnly = true;
lbKeyHint->Visible = true;
edKey->Text = 1;
edKey->Width = 145;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btClearClick(TObject* Sender)
{
reDestText->Text = "";
reSrcText->Text = "";
if (cbMethod->ItemIndex == 1) {
edKey->Text = "";
} else {
edKey->Text = 1;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::readFileInput(std::wifstream &fileName, TObject* Sender)
{
TErrorCode error;
//fileName.imbue(std::locale(""));
fileName.imbue(std::locale(fileName.getloc(), new std::codecvt_utf8<wchar_t>));
reSrcText->Lines->BeginUpdate();
reSrcText->Lines->Clear();
std::wstring str;
while (std::getline(fileName, str))
{
reSrcText->Lines->Add(UnicodeString(str.c_str()));
}
reSrcText->Lines->EndUpdate();
fileName.close();
}
void __fastcall TMainForm::saveFileInput(std::wofstream &fileName, TObject* Sender)
{
TErrorCode error;
fileName.imbue(std::locale(fileName.getloc(), new std::codecvt_utf8<wchar_t>));
std::wstring str;
for (int i = 0; i < reDestText->Lines->Count; i++)
{
fileName << reDestText->Lines->Strings[i] << std::endl;
}
fileName.close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btFileClick(TObject* Sender)
{
std::wifstream fileName;
std::wstring pathToFile;
TErrorCode error;
if (OpenTextFileDialog1->Execute()) {
pathToFile = OpenTextFileDialog1->FileName.c_str();
error = isFileTxt(pathToFile);
if (error == CORRECT) {
error = isFileExist(pathToFile);
}
if (error == CORRECT) {
fileName.open(pathToFile);
error = isFileReadable(fileName);
}
if ((error == CORRECT) &&
fileName.peek() == std::wifstream::traits_type::eof()) {
error = FILE_IS_EMPTY;
}
if (error == CORRECT) {
//reSrcText->Lines->Text = TFile::ReadAllText(pathToFile);
readFileInput(fileName, Sender);
}
if (error != CORRECT) {
Application->MessageBox(
ERR[error].c_str(), L"Ошибка!", MB_OK | MB_ICONWARNING);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::udKeyClick(TObject* Sender, TUDBtnType Button)
{
//reDestText->Text = "";
for (char n : nums) {
if (udKey->Position == n) {
if (udKey->Position > lastPos) {
udKey->Position += 1;
} else {
udKey->Position -= 1;
}
}
}
TMainForm::btCipherClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::udKeyChanging(TObject* Sender, bool &AllowChange)
{
lastPos = udKey->Position;
}
//---------------------------------------------------------------------------
int modInverse(int k, int n)
{
k = (k % n + n) % n;
for (int x = 1; x < n; x++)
{
if ((k * x) % n == 1)
return x;
}
return 1;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btCipherDecimation(TObject* Sender){
int k = udKey->Position;
int n = 33;
UnicodeString input = reSrcText->Text;
UnicodeString output = "";
for (int i = 1; i <= input.Length(); i++) {
wchar_t ch = input[i];
wchar_t encCh = ch;
if ((ch >= L'А' && ch <= L'Я') || ch == L'Ё') {
int index;
if (ch == L'Ё')
index = 6;
else if (ch <= L'Е')
index = ch - L'А';
else
index = ch - L'А' + 1;
int encIndex = (index * k) % n;
if (encIndex == 6)
encCh = L'Ё';
else if (encIndex < 6)
encCh = L'А' + encIndex;
else
encCh = L'А' + encIndex - 1;
} else if ((ch >= L'а' && ch <= L'я') || ch == L'ё') {
int index;
if (ch == L'ё')
index = 6;
else if (ch <= L'е')
index = ch - L'а';
else
index = ch - L'а' + 1;
int encIndex = (index * k) % n;
if (encIndex == 6)
encCh = L'ё';
else if (encIndex < 6)
encCh = L'а' + encIndex;
else
encCh = L'а' + encIndex - 1;
}
output += encCh;
}
reDestText->Text = output;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btDeCipherDecimation(TObject* Sender){
int k = udKey->Position;
int n = 33;
int kInv = modInverse(k, n);
UnicodeString input = reSrcText->Text;
UnicodeString output = "";
for (int i = 1; i <= input.Length(); i++) {
wchar_t ch = input[i];
wchar_t encCh = ch;
if ((ch >= L'А' && ch <= L'Я') || ch == L'Ё') {
int index;
if (ch == L'Ё')
index = 6;
else if (ch <= L'Е')
index = ch - L'А';
else
index = ch - L'А' + 1;
int encIndex = (index * kInv) % n;
if (encIndex == 6)
encCh = L'Ё';
else if (encIndex < 6)
encCh = L'А' + encIndex;
else
encCh = L'А' + encIndex - 1;
} else if ((ch >= L'а' && ch <= L'я') || ch == L'ё') {
int index;
if (ch == L'ё')
index = 6;
else if (ch <= L'е')
index = ch - L'а';
else
index = ch - L'а' + 1;
int encIndex = (index * kInv) % n;
if (encIndex == 6)
encCh = L'ё';
else if (encIndex < 6)
encCh = L'а' + encIndex;
else
encCh = L'а' + encIndex - 1;
}
output += encCh;
}
reDestText->Text = output;
}
//---------------------------------------------------------------------------
int __fastcall getRussianIndex(wchar_t ch) {
int idx = RUS_UPPER.Pos(ch);
if (idx > 0) return idx - 1;
idx = RUS_LOWER.Pos(ch);
if (idx > 0) return idx - 1;
return -1;
}
// --------------------------------------------------------------------------
UnicodeString __fastcall filterRussianText(const UnicodeString &text, int &count)
{
UnicodeString result = "";
for (int i = 1; i <= text.Length(); i++) {
wchar_t ch = text[i];
if (getRussianIndex(ch) != -1) {
count++;
}
result += ch;
}
return result;
}
// --------------------------------------------------------------------------
wchar_t __fastcall shiftLetter(wchar_t ch)
{
int idx = getRussianIndex(ch);
if (idx == -1) return ch;
idx = (idx + 1) % 33;
if (RUS_UPPER.Pos(ch) > 0)
return RUS_UPPER[idx + 1];
else
return RUS_LOWER[idx + 1];
}
// --------------------------------------------------------------------------
UnicodeString __fastcall generateAutoKey(const UnicodeString &baseKey, const UnicodeString &text)
{
int ruLetters = 0;
UnicodeString result = "";
for (int i = 1; i <= baseKey.Length(); i++) {
wchar_t ch = baseKey[i];
if (getRussianIndex(ch) != -1) {
result += ch;
}
}
UnicodeString filteredKey = result;
UnicodeString resultKey = filteredKey;
UnicodeString filteredText = filterRussianText(text, ruLetters);
int diff = ruLetters - filteredKey.Length();
int i = 1;
while (i <= diff) {
wchar_t ch = filteredText[i];
int tIdx = getRussianIndex(ch);
if (tIdx != -1) {
resultKey += ch;
} else {
diff++;
}
i++;
}
return resultKey;
}
// --------------------------------------------------------------------------
void __fastcall TMainForm::btCipherVijner(TObject* Sender){
int ruLetters = 0;
UnicodeString filteredText = filterRussianText(reSrcText->Text, ruLetters);
UnicodeString autoKey = generateAutoKey(edKey->Text, filteredText);
UnicodeString result = "";
int j = 1;
for (int i = 1; i <= filteredText.Length(); i++) {
wchar_t ch = filteredText[i];
wchar_t ch2;
if (j <= autoKey.Length()) {
ch2 = autoKey[j];
}
int tIdx = getRussianIndex(ch);
int kIdx = getRussianIndex(ch2);
if (tIdx == -1 || kIdx == -1) {
result += ch;
continue;
}
int newIdx = (tIdx + kIdx) % 33;
if (RUS_UPPER.Pos(ch) > 0)
result += RUS_UPPER[newIdx + 1];
else
result += RUS_LOWER[newIdx + 1];
j++;
}
edKey->Text = autoKey;
reDestText->Text = result;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btDeCipherVijner(TObject* Sender){
int ruLetters = 0;
UnicodeString filteredText = filterRussianText(reSrcText->Text, ruLetters);
UnicodeString autoKey = generateAutoKey(edKey->Text, filteredText);
UnicodeString result = "";
int j = 1;
for (int i = 1; i <= filteredText.Length(); i++) {
wchar_t ch = filteredText[i];
wchar_t ch2;
if (j <= autoKey.Length()) {
ch2 = autoKey[j];
}
int cIdx = getRussianIndex(ch);
int kIdx = getRussianIndex(ch2);
if (cIdx == -1 || kIdx == -1) {
result += ch;
continue;
}
int newIdx = (cIdx - kIdx) % 33;
if (newIdx < 0) newIdx += 33;
if (RUS_UPPER.Pos(ch) > 0)
result += RUS_UPPER[newIdx + 1];
else
result += RUS_LOWER[newIdx + 1];
j++;
}
reDestText->Text = result;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btCipherClick(TObject* Sender)
{
if (cbMethod->ItemIndex == 0 && edKey->Text.ToInt() > 32) {
edKey->Text = "32";
}
if (cbMethod->ItemIndex == 0) {
TMainForm::btCipherDecimation(Sender);
} else {
TMainForm::btCipherVijner(Sender);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btDecipherClick(TObject *Sender)
{
reSrcText->Text = reDestText->Text;
reDestText->Text = "";
if (cbMethod->ItemIndex == 0) {
TMainForm::btDeCipherDecimation(Sender);
} else {
TMainForm::btDeCipherVijner(Sender);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::reSrcTextChange(TObject *Sender)
{
reDestText->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::N4Click(TObject *Sender)
{
std::wofstream fileName;
std::wstring pathToFile;
TErrorCode error;
if (SaveTextFileDialog1->Execute()) {
pathToFile = SaveTextFileDialog1->FileName.c_str();
error = isFileTxt(pathToFile);
if (error == CORRECT) {
error = isFileExist(pathToFile);
}
if (error == CORRECT) {
fileName.open(pathToFile);
error = isFileWritable(fileName);
}
if (error == CORRECT) {
saveFileInput(fileName, Sender);
}
if (error != CORRECT) {
Application->MessageBox(
ERR[error].c_str(), L"Ошибка!", MB_OK | MB_ICONWARNING);
}
}
}
//---------------------------------------------------------------------------