#include "CryptHelper.h" #include #include #include #include #include #include #include #include #include #include #include CryptHelper::CryptHelper() { } CryptHelper::~CryptHelper() { } int CryptHelper::Base64DecodeWrap(string str,unsigned char** out) { typedef BIO_METHOD* (*My_BIO_f_base64)(void); char szFilePath[MAX_PATH + 1] = { 0 }; GetModuleFileName(NULL, szFilePath, MAX_PATH); (strrchr(szFilePath, '\\'))[1] = 0; std::string path = szFilePath; HMODULE hDLL = LoadLibrary((path+"\\libeay32.dll").c_str()); // 加载 DLL if (hDLL == NULL) { printf( "Failed to load DLL...\n" ); return -1; } // 获取函数地址 My_BIO_f_base64 bioBase64 = reinterpret_cast(GetProcAddress(hDLL, "BIO_f_base64")); if (bioBase64 == NULL) { printf("Failed to get function address...\n"); FreeLibrary(hDLL); // 释放 DLL return -1; } unsigned char buffer[1024]; BIO* b64 = NULL; BIO* bio = NULL; b64 = BIO_new(bioBase64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bio = BIO_new_mem_buf(str.c_str(), str.length()); bio = BIO_push(b64, bio); int outlength = BIO_read(bio, buffer, str.length()); *out = new unsigned char[outlength]; memcpy(*out,buffer,outlength); BIO_free_all(bio); FreeLibrary(hDLL); // 释放 DLL return outlength; } int CryptHelper::AesDecode(unsigned char* ct, int ctLength, unsigned char* key, int keyLength, unsigned char* iv, int ivLength, unsigned char* tag, int tagLength,unsigned char** out) { unsigned char buffer[1024]; unsigned char tagBuffer[1024]; int outLength,rv,tagOutLength; EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN,ivLength, NULL); EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv); EVP_DecryptUpdate(ctx, buffer, &outLength, ct,ctLength); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tagLength, tag); rv = EVP_DecryptFinal_ex(ctx, tagBuffer, &tagOutLength); if (rv > 0 && tagOutLength>0) { *out = new unsigned char[outLength+tagOutLength]; memcpy(*out,buffer,outLength); memcpy((*out) + outLength, tagBuffer, tagOutLength); } else { *out = new unsigned char[outLength]; memcpy(*out, buffer, outLength); } EVP_CIPHER_CTX_free(ctx); return outLength+tagOutLength; } string CryptHelper::DesEncode(string str, string strkey) { if (str.empty())return 0; const_DES_cblock key = { 0 }; for (int i = 0; i < strkey.length() && i < 8; i++) { key[i] = strkey[i]; } int strLength = str.length(); queue srcdata; for (int i = 0; i < strLength; i++) { srcdata.push(str[i]); } DES_key_schedule key_sch; DES_set_key(&key, &key_sch); vector outdata; while (!srcdata.empty()) { unsigned char datatemp[8]; unsigned char outtemp[8]; int ids = 0; for (int i = 0; i < 8; i++) { if (!srcdata.empty()) { datatemp[i] = srcdata.front(); srcdata.pop(); ids++; } } if (ids < 8) { for (int s = ids; s < 8; s++) { datatemp[s] = 8 - ids; } } DES_ecb_encrypt((const_DES_cblock*)&datatemp, (DES_cblock*)&outtemp, &key_sch, DES_ENCRYPT); for (int j = 0; j < 8; j++) { outdata.push_back(outtemp[j]); } } if (srcdata.size() % 8 == 0) { unsigned char datatemp[8]; unsigned char outtemp[8]; memset(datatemp, 8, 8); DES_ecb_encrypt((const_DES_cblock*)&datatemp, (DES_cblock*)&outtemp, &key_sch, DES_ENCRYPT); for (int j = 0; j < 8; j++) { outdata.push_back(outtemp[j]); } } char temp[10]; stringstream ss; for (size_t i = 0; i < outdata.size(); i++) { sprintf_s(temp, sizeof(temp), "%02x", outdata[i]); ss << temp; } return ss.str().c_str(); }