-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit2.cpp
More file actions
408 lines (375 loc) · 11.3 KB
/
Copy pathUnit2.cpp
File metadata and controls
408 lines (375 loc) · 11.3 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
//---------------------------------------------------------------------------
#include <vcl.h>
#include <System.IOUtils.hpp>
#pragma hdrstop
#include <set>
#include <iostream>
#include <fstream>
#include <bitset>
#include <stdio.h>
#include <cstdint>
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm* MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner) {}
//---------------------------------------------------------------------------
char* src;
char* key;
char* res;
int size = 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 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::ifstream &fileName)
{
TErrorCode error;
error = CORRECT;
if (!fileName.is_open()) {
error = FILE_NOT_READABLE;
}
return error;
}
TErrorCode __fastcall isFileWritable(const std::ofstream &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::btClearClick(TObject* Sender)
{
reDestText->Text = "";
reGenKey->Text = "";
reSrcText->Text = "";
edKey->Text = "";
}
//---------------------------------------------------------------------------
char* resizeArray(char* arr, int ¤tCapacity)
{
int newCapacity = currentCapacity * 2;
char* newArr = new char[newCapacity];
for (int i = 0; i < currentCapacity; i++) {
newArr[i] = arr[i];
}
delete[] arr;
currentCapacity = newCapacity;
return newArr;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::readFileInput(
std::ifstream &fileName, TObject* Sender)
{
String binaryStr = "";
char ch;
int i = 0;
size = 65535;
src = new char[size];
unsigned char byte;
while (fileName.read(&ch, 1)) {
if (i >= size) {
src = resizeArray(src, size);
}
src[i] = ch;
i++;
}
size = i;
for (int j = 0; j < 10; j++) {
ch = src[j];
for (int i = 7; i >= 0; i--) {
binaryStr += ((ch >> i) & 1) ? "1" : "0";
}
binaryStr += " ";
}
binaryStr += "\n\n\n";
for (int j = size - 10; j < size; j++) {
ch = src[j];
for (int i = 7; i >= 0; i--) {
binaryStr += ((ch >> i) & 1) ? "1" : "0";
}
binaryStr += " ";
}
reSrcText->Lines->Text = binaryStr;
fileName.close();
}
//---------------------------------------------------------------------------
/* */
void __fastcall TMainForm::saveFileInput(
std::ofstream &fileName, TObject* Sender)
{
fileName.write(res, size);
//for (int i = 0; i < size; i++) {
// fileName.put(inf[i]);
//}
Application->MessageBox(
L"Файл сохранен!", L"Готово!", MB_OK | MB_ICONWARNING);
fileName.close();
}
//---------------------------------------------------------------------------
/* */
uint64_t __fastcall lfsr(uint64_t seed, int steps)
{
const int n = 33;
const int taps[] = { 32, 12 };
uint64_t state = seed & ((1ULL << n) - 1);
for (int i = 0; i < steps; i++) {
int feedback = 0;
for (int t : taps) {
feedback ^= (state >> t) & 1;
}
int out = state & 1;
state = (state << 1) | (static_cast<uint64_t>(feedback));
}
return state;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btFileClick(TObject* Sender)
{
//std::ifstream fileName;
std::wstring pathToFile;
TErrorCode error;
if (OpenTextFileDialog1->Execute()) {
pathToFile = OpenTextFileDialog1->FileName.c_str();
std::ifstream fileName(pathToFile, std::ios::binary);
error = CORRECT;
if (error == CORRECT) {
error = isFileExist(pathToFile);
}
if (error == CORRECT) {
//fileName.open(pathToFile, std::ios::binary);
error = isFileReadable(fileName);
}
if ((error == CORRECT) &&
fileName.peek() == std::ifstream::traits_type::eof()) {
error = FILE_IS_EMPTY;
}
if (error == CORRECT) {
readFileInput(fileName, Sender);
}
if (error != CORRECT) {
Application->MessageBox(
ERR[error].c_str(), L"Ошибка!", MB_OK | MB_ICONWARNING);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btCipherClick(TObject* Sender)
{
if (edKey->Text.Length() < 33) {
Application->MessageBox(
L"Длинна меньше 33!", L"Ошибка!", MB_OK | MB_ICONWARNING);
} else {
if (reSrcText->Text.Length() != 0) {
uint64_t seed = 0;
key = new char[size];
res = new char[size];
for (int i = edKey->Text.Length(); i > 0; i--) {
if (edKey->Text[i] == '1') {
seed |= (1ULL << (edKey->Text.Length() - i));
}
}
for (int i = 0; i < size; i++) {
unsigned char currentByte = src[i];
unsigned char keyByte = 0;
unsigned char resByte = 0;
for (int bit = 7; bit >= 0; bit--) {
seed = lfsr(seed, 1);
int lfsrBit = (seed >> 31) & 1;
if (lfsrBit) {
keyByte |= (1 << bit);
}
int fileBit = (currentByte >> bit) & 1;
int xorResult = fileBit ^ lfsrBit;
if (xorResult) {
resByte |= (1 << bit);
}
}
key[i] = keyByte;
res[i] = resByte;
}
String binaryStrKey = "";
String binaryStrRes = "";
for (int j = 0; j < 10; j++) {
for (int b = 7; b >= 0; b--) {
binaryStrKey += ((key[j] >> b) & 1) ? "1" : "0";
binaryStrRes += ((res[j] >> b) & 1) ? "1" : "0";
}
binaryStrKey += " ";
binaryStrRes += " ";
}
binaryStrKey += "\n\n\n";
binaryStrRes += "\n\n\n";
for (int j = size - 10; j < size; j++) {
for (int b = 7; b >= 0; b--) {
binaryStrKey += ((key[j] >> b) & 1) ? "1" : "0";
binaryStrRes += ((res[j] >> b) & 1) ? "1" : "0";
}
binaryStrKey += " ";
binaryStrRes += " ";
}
reGenKey->Text = binaryStrKey;
reDestText->Text = binaryStrRes;
} else {
Application->MessageBox(
L"Выберите файл!", L"Ошибка!", MB_OK | MB_ICONWARNING);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btDecipherClick(TObject* Sender)
{
if (edKey->Text.Length() < 33) {
Application->MessageBox(
L"Длинна меньше 33!", L"Ошибка!", MB_OK | MB_ICONWARNING);
} else {
if (reSrcText->Text.Length() != 0) {
uint64_t seed = 0;
key = new char[size];
res = new char[size];
for (int i = edKey->Text.Length(); i > 0; i--) {
if (edKey->Text[i] == '1') {
seed |= (1ULL << (edKey->Text.Length() - i));
}
}
for (int i = 0; i < size; i++) {
unsigned char currentByte = src[i];
unsigned char keyByte = 0;
unsigned char resByte = 0;
for (int bit = 7; bit >= 0; bit--) {
seed = lfsr(seed, 1);
int lfsrBit = (seed >> 31) & 1;
if (lfsrBit) {
keyByte |= (1 << bit);
}
int fileBit = (currentByte >> bit) & 1;
int xorResult = fileBit ^ lfsrBit;
if (xorResult) {
resByte |= (1 << bit);
}
}
key[i] = keyByte;
res[i] = resByte;
}
String binaryStrKey = "";
String binaryStrRes = "";
for (int j = 0; j < 10; j++) {
for (int b = 7; b >= 0; b--) {
binaryStrKey += ((key[j] >> b) & 1) ? "1" : "0";
binaryStrRes += ((res[j] >> b) & 1) ? "1" : "0";
}
binaryStrKey += " ";
binaryStrRes += " ";
}
binaryStrKey += "\n\n\n";
binaryStrRes += "\n\n\n";
for (int j = size - 10; j < size; j++) {
for (int b = 7; b >= 0; b--) {
binaryStrKey += ((key[j] >> b) & 1) ? "1" : "0";
binaryStrRes += ((res[j] >> b) & 1) ? "1" : "0";
}
binaryStrKey += " ";
binaryStrRes += " ";
}
reGenKey->Text = binaryStrKey;
reDestText->Text = binaryStrRes;
} else {
Application->MessageBox(
L"Выберите файл!", L"Ошибка!", MB_OK | MB_ICONWARNING);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::reSrcTextChange(TObject* Sender)
{
reDestText->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::N4Click(TObject* Sender)
{
//std::ofstream fileName;
std::wstring pathToFile;
TErrorCode error;
if (SaveTextFileDialog1->Execute()) {
pathToFile = SaveTextFileDialog1->FileName.c_str();
std::ofstream fileName(pathToFile, std::ios::binary);
error = CORRECT;
if (error == CORRECT) {
//error = isFileExist(pathToFile);
}
if (error == CORRECT) {
//fileName.open(pathToFile, std::ios::binary);
error = isFileWritable(fileName);
}
if (error == CORRECT) {
saveFileInput(fileName, Sender);
}
if (error != CORRECT) {
Application->MessageBox(
ERR[error].c_str(), L"Ошибка!", MB_OK | MB_ICONWARNING);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::edKeyKeyPress(TObject* Sender, System::WideChar &Key)
{
if (Key != '0' && Key != '1' && Key != VK_DELETE && Key != 0x08) {
Key = NULL;
}
}
//---------------------------------------------------------------------------