210 lines
7.2 KiB
C++
210 lines
7.2 KiB
C++
#include "Crypto.h"
|
||||
|
|
|
|||
|
|
#define NOMINMAX
|
|||
|
|
#include <windows.h>
|
|||
|
|
|
|||
|
|
#include <bcrypt.h>
|
|||
|
|
|
|||
|
|
#include <string>
|
|||
|
|
#include <vector>
|
|||
|
|
|
|||
|
|
#pragma comment(lib, "bcrypt.lib")
|
|||
|
|
|
|||
|
|
namespace updater {
|
|||
|
|
|
|||
|
|
std::vector<unsigned char> Crypto::PadKey(const std::vector<unsigned char>& key, size_t target) {
|
|||
|
|
if (key.size() >= target) {
|
|||
|
|
return std::vector<unsigned char>(key.begin(), key.begin() + target);
|
|||
|
|
}
|
|||
|
|
std::vector<unsigned char> padded(target, 0);
|
|||
|
|
for (size_t i = 0; i < target; ++i) padded[i] = key[i % key.size()];
|
|||
|
|
return padded;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool Crypto::Base64Decode(const std::string& input, std::vector<unsigned char>& out) {
|
|||
|
|
static const int table[256] = {
|
|||
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|||
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|||
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
|
|||
|
|
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
|
|||
|
|
-1, 0, 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,-1,-1,-1,-1,-1,
|
|||
|
|
-1,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,-1,-1,-1,-1,-1
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
out.clear();
|
|||
|
|
unsigned int buffer = 0;
|
|||
|
|
int bits = 0;
|
|||
|
|
for (unsigned char c : input) {
|
|||
|
|
int value = table[c];
|
|||
|
|
if (value < 0) {
|
|||
|
|
if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') continue;
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
buffer = (buffer << 6) | static_cast<unsigned int>(value);
|
|||
|
|
bits += 6;
|
|||
|
|
if (bits >= 8) {
|
|||
|
|
bits -= 8;
|
|||
|
|
out.push_back(static_cast<unsigned char>((buffer >> bits) & 0xFF));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::string Crypto::Base64Encode(const std::vector<unsigned char>& input) {
|
|||
|
|
static const char* chars =
|
|||
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|||
|
|
std::string out;
|
|||
|
|
out.reserve(((input.size() + 2) / 3) * 4);
|
|||
|
|
for (size_t i = 0; i < input.size(); i += 3) {
|
|||
|
|
unsigned int n = static_cast<unsigned int>(input[i]) << 16;
|
|||
|
|
if (i + 1 < input.size()) n |= static_cast<unsigned int>(input[i + 1]) << 8;
|
|||
|
|
if (i + 2 < input.size()) n |= static_cast<unsigned int>(input[i + 2]);
|
|||
|
|
|
|||
|
|
out.push_back(chars[(n >> 18) & 0x3F]);
|
|||
|
|
out.push_back(chars[(n >> 12) & 0x3F]);
|
|||
|
|
out.push_back((i + 1 < input.size()) ? chars[(n >> 6) & 0x3F] : '=');
|
|||
|
|
out.push_back((i + 2 < input.size()) ? chars[n & 0x3F] : '=');
|
|||
|
|
}
|
|||
|
|
return out;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool Crypto::DecryptAes(const std::string& key, const std::string& ciphertext, std::string& out,
|
|||
|
|
std::string& err) {
|
|||
|
|
std::vector<unsigned char> data;
|
|||
|
|
if (!Base64Decode(ciphertext, data)) {
|
|||
|
|
err = "密文 base64 解码失败";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
const size_t kNonceSize = 12;
|
|||
|
|
const size_t kTagSize = 16;
|
|||
|
|
if (data.size() < kNonceSize + kTagSize) {
|
|||
|
|
err = "密文长度不足";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::vector<unsigned char> keyBytes =
|
|||
|
|
PadKey(std::vector<unsigned char>(key.begin(), key.end()), 32);
|
|||
|
|
|
|||
|
|
BCRYPT_ALG_HANDLE alg = nullptr;
|
|||
|
|
NTSTATUS status = BCryptOpenAlgorithmProvider(&alg, BCRYPT_AES_ALGORITHM, nullptr, 0);
|
|||
|
|
if (status < 0) {
|
|||
|
|
err = "无法初始化 AES";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
status = BCryptSetProperty(alg, BCRYPT_CHAINING_MODE,
|
|||
|
|
reinterpret_cast<PUCHAR>(const_cast<wchar_t*>(BCRYPT_CHAIN_MODE_GCM)),
|
|||
|
|
static_cast<ULONG>((wcslen(BCRYPT_CHAIN_MODE_GCM) + 1) * sizeof(wchar_t)),
|
|||
|
|
0);
|
|||
|
|
if (status < 0) {
|
|||
|
|
BCryptCloseAlgorithmProvider(alg, 0);
|
|||
|
|
err = "无法设置 GCM 模式";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BCRYPT_KEY_DATA_BLOB:magic(4) + version(4) + cbKeyData(4) + key bytes
|
|||
|
|
const ULONG kHeaderSize = 12;
|
|||
|
|
std::vector<unsigned char> blob(kHeaderSize + keyBytes.size(), 0);
|
|||
|
|
ULONG magic = 0x4D42444B; // 'KDBM'
|
|||
|
|
ULONG version = 1;
|
|||
|
|
ULONG keyLen = static_cast<ULONG>(keyBytes.size());
|
|||
|
|
memcpy(blob.data(), &magic, sizeof(magic));
|
|||
|
|
memcpy(blob.data() + 4, &version, sizeof(version));
|
|||
|
|
memcpy(blob.data() + 8, &keyLen, sizeof(keyLen));
|
|||
|
|
memcpy(blob.data() + kHeaderSize, keyBytes.data(), keyBytes.size());
|
|||
|
|
|
|||
|
|
BCRYPT_KEY_HANDLE keyHandle = nullptr;
|
|||
|
|
status = BCryptImportKey(alg, nullptr, BCRYPT_KEY_DATA_BLOB, &keyHandle, nullptr, 0,
|
|||
|
|
blob.data(), static_cast<ULONG>(blob.size()), 0);
|
|||
|
|
if (status < 0) {
|
|||
|
|
BCryptCloseAlgorithmProvider(alg, 0);
|
|||
|
|
err = "密钥导入失败";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 密文布局:nonce || ciphertext || tag,tag 单独传给 bcrypt,不能算进密文长度
|
|||
|
|
size_t cipherLen = data.size() - kNonceSize - kTagSize;
|
|||
|
|
std::vector<unsigned char> nonce(data.begin(), data.begin() + kNonceSize);
|
|||
|
|
std::vector<unsigned char> tag(data.end() - kTagSize, data.end());
|
|||
|
|
std::vector<unsigned char> plain(cipherLen, 0);
|
|||
|
|
|
|||
|
|
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO info;
|
|||
|
|
BCRYPT_INIT_AUTH_MODE_INFO(info);
|
|||
|
|
info.pbNonce = nonce.data();
|
|||
|
|
info.cbNonce = static_cast<ULONG>(nonce.size());
|
|||
|
|
info.pbTag = tag.data();
|
|||
|
|
info.cbTag = static_cast<ULONG>(tag.size());
|
|||
|
|
|
|||
|
|
ULONG plainLen = 0;
|
|||
|
|
status = BCryptDecrypt(keyHandle, data.data() + kNonceSize, static_cast<ULONG>(cipherLen),
|
|||
|
|
&info, nullptr, 0, plain.data(), static_cast<ULONG>(plain.size()),
|
|||
|
|
&plainLen, 0);
|
|||
|
|
|
|||
|
|
BCryptDestroyKey(keyHandle);
|
|||
|
|
BCryptCloseAlgorithmProvider(alg, 0);
|
|||
|
|
|
|||
|
|
if (status < 0) {
|
|||
|
|
err = "解密失败(密钥不正确或数据被篡改)";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
out.assign(reinterpret_cast<const char*>(plain.data()), plainLen);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool Crypto::DecryptRc4(const std::string& key, const std::string& ciphertext, std::string& out,
|
|||
|
|
std::string& err) {
|
|||
|
|
std::vector<unsigned char> data;
|
|||
|
|
if (!Base64Decode(ciphertext, data)) {
|
|||
|
|
err = "密文 base64 解码失败";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
if (key.empty()) {
|
|||
|
|
err = "RC4 密钥为空";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
unsigned char sbox[256];
|
|||
|
|
for (int i = 0; i < 256; ++i) sbox[i] = static_cast<unsigned char>(i);
|
|||
|
|
|
|||
|
|
unsigned char j = 0;
|
|||
|
|
for (int i = 0; i < 256; ++i) {
|
|||
|
|
j = static_cast<unsigned char>(j + sbox[i] + key[i % key.size()]);
|
|||
|
|
unsigned char tmp = sbox[i];
|
|||
|
|
sbox[i] = sbox[j];
|
|||
|
|
sbox[j] = tmp;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
unsigned char i = 0;
|
|||
|
|
j = 0;
|
|||
|
|
out.clear();
|
|||
|
|
out.reserve(data.size());
|
|||
|
|
for (unsigned char c : data) {
|
|||
|
|
i = static_cast<unsigned char>(i + 1);
|
|||
|
|
j = static_cast<unsigned char>(j + sbox[i]);
|
|||
|
|
unsigned char tmp = sbox[i];
|
|||
|
|
sbox[i] = sbox[j];
|
|||
|
|
sbox[j] = tmp;
|
|||
|
|
out.push_back(static_cast<char>(c ^ sbox[(sbox[i] + sbox[j]) & 0xFF]));
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool Crypto::Decrypt(EncryptType type, const std::string& key, const std::string& ciphertext,
|
|||
|
|
std::string& out, std::string& err) {
|
|||
|
|
if (type == EncryptType::None) {
|
|||
|
|
out = ciphertext;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
if (key.empty()) {
|
|||
|
|
err = "未配置通信密钥";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
if (type == EncryptType::AES) return DecryptAes(key, ciphertext, out, err);
|
|||
|
|
return DecryptRc4(key, ciphertext, out, err);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|