-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryption.cpp
39 lines (31 loc) · 999 Bytes
/
Encryption.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
#include "Encryption.h"
void XOR(std::string& data, const std::string& key) {
for (size_t i = 0; i != data.size(); i++)
data[i] ^= key[i % key.size()];
}
int AESDecrypt(std::vector<BYTE>& payload, char* key, size_t keylen) {
HCRYPTPROV hProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;
if (!CryptAcquireContextW(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
return -1;
}
if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash)) {
return -1;
}
if (!CryptHashData(hHash, (BYTE*)key, (DWORD)keylen, 0)) {
return -1;
}
if (!CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey)) {
return -1;
}
DWORD dataSize = static_cast<DWORD>(payload.size());
DWORD* pDataSize = &dataSize;
if (!CryptDecrypt(hKey, (HCRYPTHASH)NULL, 0, 0, (BYTE*)payload.data(), pDataSize)) {
return -1;
}
CryptReleaseContext(hProv, 0);
CryptDestroyHash(hHash);
CryptDestroyKey(hKey);
return 0;
}