-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChineseEncryptor.cpp
More file actions
267 lines (237 loc) · 6.88 KB
/
ChineseEncryptor.cpp
File metadata and controls
267 lines (237 loc) · 6.88 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
#define VERSION "v0.5.1(内部预览版本)"
#define INTRODUCTION "大概是世界上第一个利用汉字进行文件加密的算法。"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
#include <bitset>
#include <fstream>
using namespace std;
map<int, string> dic;
map<string, int> dicDic;
int bin2dec(vector<bool> binBits) {
int result = 0;
for (int i = 0; i < binBits.size(); i += 1) {
if (binBits[binBits.size() - 1 - i]) {
result += pow(2, i);
}
}
return result;
}
vector<bool> dec2bin(int decNumber, int maxLength) {
vector<bool> result;
int num = decNumber;
for (int i = 0; num != 0; i++) {
result.push_back(num % 2);
num /= 2;
}
result.resize(maxLength, 0);
reverse(result.begin(), result.end());
return result;
}
double seededRandom(long long int seed) {
double random = ((seed * 9301 + 49297) % 233280) / double(233280);
return round(random * pow(10, 8)) / pow(10, 8);
}
void init(string keyString) {
int range1 = 0xf7 - 0xb0;
int range2 = 0xfe - 0xa1;
char code1, code2;
char ch[3] = { 0 };
string chs;
for (int i = 0; i < 4098; i += 1) {
if (i == 1672) {
code1 = 0xe3;
code2 = 0xa7;
}
else if (i == 2879) {
code1 = 0xe4;
code2 = 0xa8;
}
else {
code1 = i % range1 + 0xb0;
code2 = i % range2 + 0xa1;
}
ch[0] = code1;
ch[1] = code2;
chs = string(ch);
dic[i] = chs;
dicDic[chs] = i;
}
if (keyString.length() > 0) {
vector<double> randomNum(keyString.length(), NULL);
for (int i = dic.size() - 1; i >= 0; i -= 1) {
int keyIndex = i % keyString.length();
int seed = (randomNum[keyIndex] != NULL) ? round(randomNum[keyIndex] * pow(10, 8)) : int(keyString[keyIndex]);
randomNum[keyIndex] = seededRandom(seed);
int target = floor(randomNum[keyIndex] * (i + 1));
swap(dic[i], dic[target]);
dicDic[dic[i]] = i;
dicDic[dic[target]] = target;
}
}
}
void die(string message, int exitCode) {
if (exitCode == 0) {
cout << message << endl;
}
else {
cout << "\033[31;1m" << message << "\033[0m" << endl;
}
cout << "作者主页:https://www.jiuxiaoqi.top/" << endl;
exit(exitCode);
}
vector<bool> readFileAsBits(string filePath) {
ifstream file(filePath, ios::in | ios::binary);
if (!file.is_open()) {
die("无法打开文件:" + filePath, 2001);
}
file.seekg(0, ios::end);
size_t fileLength = file.tellg();
unsigned char* fileData = new unsigned char[fileLength]();
file.seekg(0, ios::beg);
file.read(reinterpret_cast<char*>(fileData), sizeof(char) * fileLength);
file.close();
vector<bool> result;
int refreshLength = floor(fileLength / 100);
for (size_t i = 0; i < fileLength; i += 1) {
bitset<8> bits;
bits = fileData[i];
for (size_t j = 0; j < 8; j += 1) {
result.push_back(bits[8 - j - 1]);
}
if (i % refreshLength == 0) {
cout << "\r读取文件 已完成 " << floor(100 * i / fileLength) << "%";
}
}
cout << "\r读取文件 已完成 100%" << endl;
return result;
}
void writeFileWithString(string filePath, string fileContent) {
ofstream file(filePath, ios::out | ios::binary);
if (!file.is_open()) {
die("无法打开文件:" + filePath, 2002);
}
file << fileContent;
file.close();
cout << "\r写入文件 已完成 100%" << endl;
}
string readFileAsString(string filePath) {
ifstream file(filePath, ios::in);
if (!file.is_open()) {
die("无法打开文件:" + filePath, 2003);
}
string fileData;
file >> fileData;
file.close();
cout << "\r读取文件 已完成 100%" << endl;
return fileData;
}
void writeFileWithBits(string filePath, vector<bool> fileContent) {
ofstream file(filePath, ios::out | ios::binary);
if (!file.is_open()) {
die("无法打开文件:" + filePath, 2004);
}
int refreshLength = floor(fileContent.size() / 800) * 8;
for (int i = 0; i < fileContent.size(); i += 8) {
vector<bool> eightBits(8);
vector<bool>::iterator first = fileContent.begin() + i;
vector<bool>::iterator last = fileContent.begin() + i + 8;
eightBits.assign(first, last);
file << char(bin2dec(eightBits));
if (i % refreshLength == 0) {
cout << "\r写入文件 已完成 " << floor(100 * i / fileContent.size()) << "%";
}
}
cout << "\r写入文件 已完成 100%" << endl;
file.close();
}
string encode(vector<bool> sourceBits) {
string result = "";
int refreshLength = floor(sourceBits.size() / 1200) * 12;
for (int i = 0; i < sourceBits.size(); i += 12) {
vector<bool> twelveBits(12);
vector<bool>::iterator first = sourceBits.begin() + i;
vector<bool>::iterator last = (i + 12 < sourceBits.size()) ? (sourceBits.begin() + i + 12) : sourceBits.end();
twelveBits.assign(first, last);
twelveBits.resize(12, 0);
int index = bin2dec(twelveBits);
result += dic[index];
for (int j = 0; j < floor((12 - (last - first)) / 8); j += 1) {
result += dic[4096];
}
if (i % refreshLength == 0) {
cout << "\r加密 已完成 " << floor(100 * i / sourceBits.size()) << "%";
}
}
while ((result.length() * 12 / 2) % 8 != 0) {
result += dic[4097];
}
cout << "\r加密 已完成 100%" << endl;
return result;
}
vector<bool> decode(string encodedString) {
vector<bool> result;
int refreshLength = floor(encodedString.size() / 200) * 2;
for (int i = 0; i < encodedString.size(); i += 2) {
string chr = encodedString.substr(i, 2);
int index = dicDic[chr];
if (index == 4096) {
result.erase(result.end() - 8, result.end());
continue;
}
else if (index == 4097) {
break;
}
vector<bool> twelveBits(12);
twelveBits = dec2bin(index, 12);
result.insert(result.end(), twelveBits.begin(), twelveBits.end());
if (i % refreshLength == 0) {
cout << "\r解密 已完成 " << floor(100 * i / encodedString.size()) << "%";
}
}
result.resize(floor(result.size() / 8) * 8);
cout << "\r解密 已完成 100%" << endl;
return result;
}
int main(int argc, char* argv[]) {
cout << "Chinese Encryptor " << VERSION << endl;
cout << INTRODUCTION << endl;
cout << "====================================================" << endl;
string taskName, inputPath, outputPath, key;
if (argc == 1) {
cout << "参数 1:encrypt(加密)/ decrypt(解密);" << endl;
cout << "参数 2:已存在文件的绝对路径;" << endl;
cout << "参数 3:生成文件的绝对路径;" << endl;
cout << "(可选)参数 4:使用的密钥字符串。" << endl;
exit(0);
}
else if (argc == 4 || argc == 5) {
taskName = argv[1];
inputPath = argv[2];
outputPath = argv[3];
key = "";
if (argc == 5) {
key = argv[4];
}
}
else {
die("参数错误。", 1001);
}
cout << "初始化 进行中";
init(key);
cout << "\r初始化 已完成" << endl;
if (taskName == "encrypt") {
writeFileWithString(outputPath, encode(readFileAsBits(inputPath)));
}
else if (taskName == "decrypt") {
writeFileWithBits(outputPath, decode(readFileAsString(inputPath)));
}
else {
die("参数错误。", 1002);
}
die("已顺利完成所有操作。", 0);
}