This repository was archived by the owner on Jan 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoding.cpp
More file actions
91 lines (78 loc) · 2.01 KB
/
Encoding.cpp
File metadata and controls
91 lines (78 loc) · 2.01 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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
/// <summary>
/// Êîäèðîâàíèå ôàéëà ñ ïîìîùüþ êîäîâîé ñòðîêè ìåòîäîì XOR.
/// </summary>
/// <param name="file_to_encode">Êîäèðóåìûé ôàéë</param>
/// <param name="encoded_file">Ôàéë âûâîäà</param>
inline void encode_text(const char* file_to_encode, const char* encoded_file)
{
size_t seed_str_size = 100;
char seed_string[100];
#pragma region Ïîëó÷åíèå êîäîâîé ñòðîêè èç ôàéëà
ifstream ifs;
ifs.open("seed string.txt");
if (!ifs || ifs.eof())
{
cout << "Can't open input seed string file or seed string is empty";
return;
}
ifs.getline(seed_string, seed_str_size);
size_t length = strlen(seed_string);
seed_str_size = (seed_str_size > length ? length : seed_str_size) - 1;
ifs.close();
#pragma endregion
#pragma region Îòêðûòèå ïîòîêîâ äëÿ ôàéëîâ ââîäà/âûâîäà
ofstream ofs;
ifs.open(file_to_encode);
ofs.open(encoded_file);
if (!ifs || ifs.eof() || !ofs)
{
cout << "Error trying to open input or output files";
return;
}
#pragma endregion
#pragma region Êîäèðîâàíèå ôàéëà è âûâîä åãî â äðóãîé ôàéë
/*
* Ñèìâîëû äî 32 íå êîäèðóþòñÿ è íå èñïîëüçóþòñÿ ïðè êîäèðîâàíèè.
* Òàê êàê ýòî óïðàâëÿþùèå ñèìâîëû è èõ ïðèñóòñòâèå ìîæåò ñêàçàòüñÿ íà öåëîñòíîñòè ôàéëà.
*
* Òàê êàê ÿ êîäèðóþ .txt ôàéëû, ïðèõîäèòñÿ îñòàíàâëèâàòü ÷òåíèå çà îäèí ñèìâîë äî eof.
* Ïîòîìó ÷òî â .txt ñóùåñòâóåò íåâèäèìàÿ òî÷êà íà êîíöå...
*
* Åñëè êîäèðîâàòü ôàéë íå â ðàçðåøåíèè .txt, ÿ ïîëàãàþ, ÷òî ýòîé ïðîáëåìû íå áóäåò
*/
size_t current_char = 0;
while (true)
{
char ch;
ifs.get(ch);
if (ifs.eof())
{
break;
}
cout << "Get - " << ch << " - " << (int)ch;
if (ch < 32)
{
ch = ch;
}
else
{
ch = ch ^ seed_string[current_char];
if (ch < 32)
{
ch = ch ^ seed_string[current_char];
}
}
cout << " - Encode to - " << ch << endl;
ofs.put(ch);
current_char = current_char + 1 == seed_str_size ? 0 : current_char + 1;
}
#pragma endregion
ifs.close();
ofs.close();
cout << "Text encoded" << endl;
}