-
Notifications
You must be signed in to change notification settings - Fork 29
/
Persi.cpp
46 lines (35 loc) · 1.15 KB
/
Persi.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
#include <windows.h>
#include <cstdint>
static inline void prng(uint16_t* state) {
*state = 2 * (*state / 181) - 163 * (*state % 181);
}
static inline uint16_t getTimeStamp() {
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
uint64_t ts;
ts = (*(uint64_t *)&ft - 0x019db1ded53e8000) / 10000000;
// 0x019db1ded53e8000 == (FILETIME)(1970-01-01 00:00:00 GMT)
// ts happens to be the POSIX timestamp of current time
return *(uint16_t *)&ts;
}
void PersiEncrypt(unsigned char *&out, size_t &outLen, const unsigned char *in, size_t inLen, bool doXor) {
static const uint32_t suffix[3] = { 0 /* ? */, 0x6789dedc, sizeof(suffix) };
uint16_t ts = getTimeStamp();
outLen = inLen + sizeof(suffix);
out = new unsigned char[outLen];
if (doXor) {
for (size_t i = 0; i < inLen; i++)
out[i] = in[i] ^ 0xc0;
} else {
memcpy(out, in, inLen);
}
memcpy(out + inLen, suffix, sizeof(suffix));
int restLen = outLen;
int i = restLen - 1;
while (restLen) {
prng(&ts);
out[i] ^= *(uint8_t *)&ts ^ (uint8_t)(outLen - restLen + 3);
--restLen;
--i;
}
}