-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.h
210 lines (190 loc) · 5.18 KB
/
worker.h
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
#pragma once
#include "resource.h"
#include "workerRC4.h"
class Worker {
WorkerRC4 rc4 = {};
HWND hDialog = nullptr;
HANDLE hInput = nullptr;
HANDLE hOutput = nullptr;
DWORD fileSize = 0;
WCHAR inputFile[260] = {};
WCHAR outputFile[260] = {};
BYTE bufferData[8000] = {};
static WINAPI DWORD staticMain(PVOID input) {
const auto worker = (Worker *) input;
worker->workerMain();
worker->closeFile();
return 0;
}
void workerMain() {
if (encrypt) {
if (!encryptPath()) {
return;
}
if (!encryptHead()) {
return;
}
} else {
if (!decryptPath()) {
return;
}
if (!decryptHead()) {
return;
}
}
DWORD realSize = 0;
DWORD loopSize = 0;
while (encryptBlock()) {
realSize += 8000;
loopSize += 1;
if (loopSize > 1000) {
loopSize = 0;
postProgress(realSize * 100.0 / fileSize);
}
}
}
int readInput(PBYTE pData, PDWORD pSize) {
if (!hInput) {
hInput = CreateFileW(inputFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (hInput == INVALID_HANDLE_VALUE) {
postError();
return 0;
}
fileSize = GetFileSize(hInput, nullptr);
if (fileSize == 0) {
postSuccess(L"输入文件长度为零");
return 0;
}
}
if (!ReadFile(hInput, pData, *pSize, pSize, nullptr)) {
postError();
return 0;
}
return 1;
}
int writeOutput(PCBYTE pData, PDWORD pSize) {
if (!hOutput) {
hOutput = CreateFileW(outputFile, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
if (hOutput == INVALID_HANDLE_VALUE) {
postError();
return 0;
}
}
if (!WriteFile(hOutput, pData, *pSize, pSize, nullptr)) {
postError();
return 0;
}
return 1;
}
void closeFile() {
if (hInput) {
CloseHandle(hInput);
hInput = nullptr;
}
if (hOutput) {
CloseHandle(hOutput);
hOutput = nullptr;
}
}
int encryptPath() {
if (PathMatchSpecW(inputFile, L"*.exe")) {
postSuccess(L"不能加密可执行文件");
return 0;
}
if (PathMatchSpecW(inputFile, L"*.1")) {
postSuccess(L"不能重复加密");
return 0;
}
wcscat_s(outputFile, L".1");
return 1;
}
int encryptHead() {
rc4.encryptZero(bufferData, 16);
DWORD realSize = 16;
if (!writeOutput(bufferData, &realSize)) {
return 0;
}
return 1;
}
int encryptBlock() {
DWORD realSize = 8000;
if (!readInput(bufferData, &realSize)) {
return 0;
}
if (realSize == 0) {
postSuccess(L"成功");
return 0;
}
rc4.encryptData(bufferData, realSize);
if (!writeOutput(bufferData, &realSize)) {
return 0;
}
return 1;
}
int decryptPath() {
if (!PathMatchSpecW(inputFile, L"*.1")) {
postSuccess(L"不是加密文件");
return 0;
}
PathRemoveExtensionW(outputFile);
return 1;
}
int decryptHead() {
DWORD readSize = 16;
if (!readInput(bufferData, &readSize)) {
return 0;
}
if (readSize != 16) {
postMessage(L"加密文件长度太短");
return 0;
}
if (!rc4.verifyZero(bufferData, 16)) {
postMessage(L"密码错误");
return 0;
}
return 1;
}
void postProgress(double value) {
swprintf_s(column2, L"进度:%.1f%%", value);
postMessage();
}
void postSuccess(PCWSTR message) {
success = 1;
postMessage(message);
}
void postError() {
const auto errorId = GetLastError();
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, errorId, 0, column2, 300, nullptr);
postMessage();
}
void postMessage(PCWSTR message) {
wcscpy_s(column2, message);
postMessage();
}
void postMessage() {
PostMessageW(hDialog, APP_UPDATE, 0, (LPARAM) this);
}
public:
Worker *next = nullptr;
int encrypt = 0;
int index = 0;
int success = 0;
WCHAR column1[260] = {};
WCHAR column2[300] = {};
void initWorker(HWND hDlg, PCWSTR file, PCSTR key, WPARAM wParam) {
hDialog = hDlg;
wcscpy_s(inputFile, file);
wcscpy_s(outputFile, file);
rc4.initKey(key);
if (wParam == ID_ENCRYPT) {
encrypt = 1;
swprintf_s(column1, L"加密:%s", file);
} else {
encrypt = 0;
swprintf_s(column1, L"解密:%s", file);
}
}
void runWorker() {
QueueUserWorkItem(staticMain, this, 0);
}
};