-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.cpp
213 lines (171 loc) · 3.97 KB
/
main.cpp
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
#include <iostream>
#include <Windows.h>
#include <Shlwapi.h>
#define CLOSE_TIME 5000
int key[4]
{
0x4C7ADF03,
0x5F30E75F,
0x1D149820,
0x3985ADF,
};
void DecryptCSO(uint32_t* v, int sz)
{
uint32_t count = sz / 4;
for (uint32_t i = 0; i < count; i += 2)
{
uint32_t v0 = v[i];
uint32_t v1 = v[i + 1];
uint32_t sum = 0x9e3779b9 * 32;
uint32_t delta = 0x9e3779b9;
for (int i = 0; i < 32; i++)
{
v1 -= ((v0 << 4) - key[2]) ^ (v0 + sum) ^ ((v0 >> 5) - key[3]);
v0 -= ((v1 << 4) - key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1]);
sum -= delta;
}
v[i] = v0;
v[i + 1] = v1;
sum = 0x9e3779b9 * 32;
}
}
void EncryptCSO(uint32_t* v, int sz)
{
uint32_t count = sz / 4;
for (uint32_t i = 0; i < count; i += 2)
{
uint32_t v0 = v[i];
uint32_t v1 = v[i + 1];
uint32_t sum = 0;
uint32_t delta = 0x9e3779b9;
for (int i = 0; i < 32; i++)
{
sum += delta;
v0 += ((v1 << 4) - key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1]);
v1 += ((v0 << 4) - key[2]) ^ (v0 + sum) ^ ((v0 >> 5) - key[3]);
}
v[i] = v0;
v[i + 1] = v1;
sum = 0;
}
}
void PrintUsage(int argc, char* argv[])
{
printf("Use -file <\"file\"> and -enc or -dec parameter. Command line:\n");
for (int i = 0; i < argc; i++)
{
printf("%s\n", argv[i]);
}
printf("Quiting...\n");
}
int main(int argc, char *argv[])
{
char inputFilePath[MAX_PATH];
char outputFilePath[MAX_PATH];
bool encrypt;
HFILE hFile;
HFILE newFile;
OFSTRUCT tOfStr;
BY_HANDLE_FILE_INFORMATION bhFileInformation;
DWORD bytesRead;
DWORD bytesWritten;
BYTE *readBuf;
if (argc < 4)
{
PrintUsage(argc, argv);
Sleep(CLOSE_TIME);
return 0;
}
if (!strcmp(argv[1], "-file"))
{
strncpy(inputFilePath, argv[2], sizeof(inputFilePath));
}
else
{
PrintUsage(argc, argv);
Sleep(CLOSE_TIME);
return 0;
}
if (!strcmp(argv[3], "-enc"))
{
encrypt = true;
}
else if (!strcmp(argv[3], "-dec"))
{
encrypt = false;
}
else
{
PrintUsage(argc, argv);
Sleep(CLOSE_TIME);
return 0;
}
hFile = OpenFile(inputFilePath, &tOfStr, OF_READWRITE);
if (hFile == HFILE_ERROR)
{
CloseHandle((HANDLE)hFile);
printf("OpenFile() failed: %d. Make sure that you entered currect file path. Quiting...\n", GetLastError());
Sleep(CLOSE_TIME);
return 0;
}
GetFileInformationByHandle((HANDLE)hFile, &bhFileInformation);
int viSize = bhFileInformation.nFileSizeLow;
readBuf = new BYTE[viSize];
if (_llseek(hFile, 0 * sizeof(int), 0) != (long)(0 * sizeof(int)))
{
CloseHandle((HANDLE)hFile);
Sleep(CLOSE_TIME);
return 0;
}
if (!ReadFile((HANDLE)hFile, readBuf, viSize, &bytesRead, NULL))
{
CloseHandle((HANDLE)hFile);
printf("ReadFile() failed: %d. Make sure that you entered currect file path. Quiting...\n", GetLastError());
Sleep(CLOSE_TIME);
return 0;
}
if (encrypt)
{
EncryptCSO((uint32_t*)readBuf, viSize);
}
else
{
DecryptCSO((uint32_t*)readBuf, viSize);
}
// get file name from path
char* fileName = PathFindFileName(inputFilePath);
// remove extension
PathRemoveExtension(fileName);
// remove filename from filepath
PathRemoveFileSpecA(inputFilePath);
if (encrypt)
{
sprintf(outputFilePath, "%s\\%s_enc.cso", inputFilePath, fileName);
}
else
{
sprintf(outputFilePath, "%s\\%s_dec.cso", inputFilePath, fileName);
}
// now create decrypted file
newFile = (HFILE)CreateFile(outputFilePath,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (newFile == HFILE_ERROR)
{
printf("Could not create file: %d. Quiting...\n", GetLastError());
Sleep(CLOSE_TIME);
return 0;
}
// write decrypted data to file
WriteFile((HANDLE)newFile, readBuf, bytesRead, &bytesWritten, NULL);
// save decrypted file
CloseHandle((HANDLE)newFile);
CloseHandle((HANDLE)hFile);
printf("Operation completed.\n");
Sleep(CLOSE_TIME);
return 0;
}