KOD:
örnek kullanımı böyle:
Açıklama:
Veri, önce dinamik olarak üretilen bir AES anahtarı ile şifrelenir, ardından Base64 ile encode edilir. Çözümleme aşamasında ise, veriler önce Base64 ile decode edilir, ardından AES ile çözülerek orijinal haline getirilir.
C++:
#pragma once
#include <windows.h>
#include <wincrypt.h>
#include <string>
#include <array>
#include <random>
#include <chrono>
#include <iostream>
#include <vector>
#pragma comment(lib, "advapi32.lib")
namespace base64 {
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string encode(const std::vector<uint8_t>& data) {
std::string encoded;
encoded.reserve((data.size() + 2) / 3 * 4);
for (size_t i = 0; i < data.size(); i += 3) {
uint32_t triple = 0;
triple |= (data[i] << 16);
if (i + 1 < data.size()) {
triple |= (data[i + 1] << 8);
}
if (i + 2 < data.size()) {
triple |= data[i + 2];
}
encoded += base64_chars[(triple >> 18) & 63];
encoded += base64_chars[(triple >> 12) & 63];
encoded += (i + 1 < data.size()) ? base64_chars[(triple >> 6) & 63] : '=';
encoded += (i + 2 < data.size()) ? base64_chars[triple & 63] : '=';
}
return encoded;
}
std::vector<uint8_t> decode(const std::string& encoded) {
if (encoded.empty() || encoded.size() % 4 != 0) {
return {};
}
std::vector<uint8_t> decoded;
decoded.reserve((encoded.size() / 4) * 3);
for (size_t i = 0; i < encoded.size(); i += 4) {
uint32_t sextet_a = base64_chars.find(encoded[i]);
uint32_t sextet_b = base64_chars.find(encoded[i + 1]);
uint32_t sextet_c = (encoded[i + 2] == '=') ? 64 : base64_chars.find(encoded[i + 2]);
uint32_t sextet_d = (encoded[i + 3] == '=') ? 64 : base64_chars.find(encoded[i + 3]);
if (sextet_a == std::string::npos || sextet_b == std::string::npos ||
(sextet_c == std::string::npos && encoded[i + 2] != '=') ||
(sextet_d == std::string::npos && encoded[i + 3] != '=')) {
return {};
}
uint32_t triple = (sextet_a << 18) + (sextet_b << 12) +
((sextet_c != 64) ? (sextet_c << 6) : 0) +
((sextet_d != 64) ? sextet_d : 0);
decoded.push_back((triple >> 16) & 255);
if (encoded[i + 2] != '=') {
decoded.push_back((triple >> 8) & 255);
}
if (encoded[i + 3] != '=') {
decoded.push_back(triple & 255);
}
}
return decoded;
}
}
namespace x7f3a9 {
class q4a8f6 {
private:
HCRYPTPROV n2b9d1 = 0;
HCRYPTKEY v5c7e3 = 0;
std::array<uint8_t, 32> w1f4a8;
bool key_generated = false;
public:
q4a8f6() {
if (!key_generated) {
p6d3b9();
key_generated = true;
}
r8e2c5();
}
~q4a8f6() {
l9a4f7();
}
void p6d3b9() {
if (!n2b9d1) {
CryptAcquireContextW(&n2b9d1, nullptr, nullptr, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);
}
CryptGenRandom(n2b9d1, w1f4a8.size(), w1f4a8.data());
}
bool r8e2c5() {
if (v5c7e3) { CryptDestroyKey(v5c7e3); v5c7e3 = 0; }
if (n2b9d1) { CryptReleaseContext(n2b9d1, 0); n2b9d1 = 0; }
if (!CryptAcquireContextW(&n2b9d1, nullptr, nullptr, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
return false;
}
struct {
BLOBHEADER hdr;
DWORD size;
BYTE data[32];
} blob = {
{PLAINTEXTKEYBLOB, CUR_BLOB_VERSION, 0, CALG_AES_256},
32
};
memcpy(blob.data, w1f4a8.data(), 32);
if (!CryptImportKey(n2b9d1, (BYTE*)&blob, sizeof(blob), 0, 0, &v5c7e3)) {
return false;
}
DWORD padding = PKCS5_PADDING;
if (!CryptSetKeyParam(v5c7e3, KP_PADDING, (BYTE*)&padding, 0)) {
return false;
}
return true;
}
void l9a4f7() {
if (v5c7e3) { CryptDestroyKey(v5c7e3); v5c7e3 = 0; }
if (n2b9d1) { CryptReleaseContext(n2b9d1, 0); n2b9d1 = 0; }
}
std::string t3f8d2(const std::string& data) {
if (!v5c7e3) {
return "";
}
DWORD len = data.size();
DWORD bufLen = len + 16 - (len % 16);
std::vector<BYTE> buf(bufLen);
memcpy(buf.data(), data.c_str(), len);
if (CryptEncrypt(v5c7e3, 0, TRUE, 0, buf.data(), &len, bufLen)) {
return base64::encode(std::vector<uint8_t>(buf.begin(), buf.begin() + len));
}
return "";
}
std::string u7a3e9(const std::string& data) {
if (!v5c7e3 || data.empty()) {
return "";
}
auto decoded = base64::decode(data);
if (decoded.empty() || decoded.size() % 16 != 0) {
return "";
}
DWORD len = decoded.size();
std::vector<BYTE> buf(decoded.begin(), decoded.end());
if (CryptDecrypt(v5c7e3, 0, TRUE, 0, buf.data(), &len)) {
return std::string(reinterpret_cast<char*>(buf.data()), len);
}
return "";
}
};
inline q4a8f6& h5b8c1() {
static thread_local q4a8f6 aes;
return aes;
}
}
template<size_t N>
class g2d7f4 {
private:
std::string s9e1a6;
mutable std::string j4c8b3;
mutable bool i6f2d9 = false;
public:
constexpr g2d7f4(const char* str) : s9e1a6(str) {}
const std::string& z1c6f8() const {
if (!i6f2d9) {
j4c8b3 = x7f3a9::h5b8c1().t3f8d2(s9e1a6);
i6f2d9 = true;
}
static thread_local std::string decrypted;
decrypted = x7f3a9::h5b8c1().u7a3e9(j4c8b3);
return decrypted;
}
void k7e4a2() const {
x7f3a9::h5b8c1().r8e2c5();
i6f2d9 = false;
}
operator std::string() const { return z1c6f8(); }
friend std::ostream& operator<<(std::ostream& os, const g2d7f4& str) {
return os << str.z1c6f8();
}
std::string operator+(const std::string& other) const {
return z1c6f8() + other;
}
friend std::string operator+(const std::string& lhs, const g2d7f4& rhs) {
return lhs + rhs.z1c6f8();
}
};
#define crypt(str) g2d7f4<sizeof(str)>(str)
namespace d8f3a7 {
inline void n5b2e9() {
x7f3a9::h5b8c1().r8e2c5();
}
inline void c4a9f1(void* ptr, size_t size) {
SecureZeroMemory(ptr, size);
}
}
örnek kullanımı böyle:
C++:
#include <iostream>
#include "sdk.h"
int main() {
if (!x7f3a9::h5b8c1().r8e2c5()) {
std::cerr << "init error" << std::endl;
return 1;
}
std::cout << crypt("test") << std::endl;
return 0;
}
Açıklama:
Veri, önce dinamik olarak üretilen bir AES anahtarı ile şifrelenir, ardından Base64 ile encode edilir. Çözümleme aşamasında ise, veriler önce Base64 ile decode edilir, ardından AES ile çözülerek orijinal haline getirilir.
