代码如下
// Base64 编码函数
std::string Base64Encode(const std::vector<unsigned char>& input) {DWORD encodedLength = 0;// 先获取编码后的长度CryptBinaryToStringA(&input[0], static_cast<DWORD>(input.size()), CRYPT_STRING_BASE64, NULL, &encodedLength);std::vector<char> output(encodedLength);// 进行实际的编码操作CryptBinaryToStringA(&input[0], static_cast<DWORD>(input.size()), CRYPT_STRING_BASE64, &output[0], &encodedLength);return std::string(output.begin(), output.end() - 1); // 去掉末尾的换行符
}// Base64 解码函数
std::vector<unsigned char> Base64Decode(const std::string& input) {std::vector<unsigned char> output;DWORD decodedLength = 0;// 先获取解码后的长度CryptStringToBinaryA(input.c_str(), static_cast<DWORD>(input.length()), CRYPT_STRING_BASE64, NULL, &decodedLength, NULL, NULL);output.resize(decodedLength);// 进行实际的解码操作CryptStringToBinaryA(input.c_str(), static_cast<DWORD>(input.length()), CRYPT_STRING_BASE64, output.data(), &decodedLength, NULL, NULL);return output;
}// DES 加密函数
std::string DESEncrypt(const std::string& plainData, const std::string& key, const std::string& iv) {HCRYPTPROV hProv = 0;// 获取加密服务提供者的句柄if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {printf("CryptAcquireContext failed: %d\n", GetLastError());return "";}HCRYPTKEY hKey = 0;// 准备 DES 密钥结构struct {BLOBHEADER hdr;DWORD dwKeySize;BYTE rgbKeyData[8];} KeyBlob;KeyBlob.hdr.bType = PLAINTEXTKEYBLOB;KeyBlob.hdr.bVersion = CUR_BLOB_VERSION;KeyBlob.hdr.reserved = 0;KeyBlob.hdr.aiKeyAlg = CALG_DES;KeyBlob.dwKeySize = 8;memcpy(KeyBlob.rgbKeyData, key.c_str(), 8);// 导入 DES 密钥if (!CryptImportKey(hProv, (BYTE*)&KeyBlob, sizeof(KeyBlob), 0, 0, &hKey)) {printf("CryptImportKey failed: %d\n", GetLastError());CryptReleaseContext(hProv, 0);return "";}// 设置初始化向量BYTE desIV[8];memcpy(desIV, iv.c_str(), 8);if (!CryptSetKeyParam(hKey, KP_IV, desIV, 0)) {printf("CryptSetKeyParam (IV) failed: %d\n", GetLastError());CryptDestroyKey(hKey);CryptReleaseContext(hProv, 0);return "";}// 准备加密数据DWORD dataLen = static_cast<DWORD>(plainData.length());std::vector<unsigned char> encryptedBytes(dataLen + 8); // 考虑填充memcpy(&encryptedBytes[0], plainData.c_str(), dataLen);// 进行加密操作if (!CryptEncrypt(hKey, 0, TRUE, 0, &encryptedBytes[0], &dataLen, static_cast<DWORD>(encryptedBytes.size()))) {printf("CryptEncrypt failed: %d\n", GetLastError());CryptDestroyKey(hKey);CryptReleaseContext(hProv, 0);return "";}// 释放资源CryptDestroyKey(hKey);CryptReleaseContext(hProv, 0);// 对加密后的数据进行 Base64 编码return Base64Encode(std::vector<unsigned char>(encryptedBytes.begin(), encryptedBytes.begin() + dataLen));
}// DES 解密函数
std::string DESDecrypt(const std::string& encryptedData, const std::string& key, const std::string& iv) {HCRYPTPROV hProv = 0;// 获取加密服务提供者的句柄if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {printf("CryptAcquireContext failed: %d\n", GetLastError());return "";}HCRYPTKEY hKey = 0;// 准备 DES 密钥结构struct {BLOBHEADER hdr;DWORD dwKeySize;BYTE rgbKeyData[8];} KeyBlob;KeyBlob.hdr.bType = PLAINTEXTKEYBLOB;KeyBlob.hdr.bVersion = CUR_BLOB_VERSION;KeyBlob.hdr.reserved = 0;KeyBlob.hdr.aiKeyAlg = CALG_DES;KeyBlob.dwKeySize = 8;memcpy(KeyBlob.rgbKeyData, key.c_str(), 8);// 导入 DES 密钥if (!CryptImportKey(hProv, (BYTE*)&KeyBlob, sizeof(KeyBlob), 0, 0, &hKey)) {printf("CryptImportKey failed: %d\n", GetLastError());CryptReleaseContext(hProv, 0);return "";}// 设置初始化向量BYTE desIV[8];memcpy(desIV, iv.c_str(), 8);if (!CryptSetKeyParam(hKey, KP_IV, desIV, 0)) {printf("CryptSetKeyParam (IV) failed: %d\n", GetLastError());CryptDestroyKey(hKey);CryptReleaseContext(hProv, 0);return "";}// 对 Base64 编码的加密数据进行解码std::vector<unsigned char> encryptedBytes = Base64Decode(encryptedData);DWORD dataLen = static_cast<DWORD>(encryptedBytes.size());std::vector<unsigned char> decryptedBytes(dataLen);memcpy(decryptedBytes.data(), encryptedBytes.data(), dataLen);// 进行解密操作if (!CryptDecrypt(hKey, 0, TRUE, 0, decryptedBytes.data(), &dataLen)) {printf("CryptDecrypt failed: %d\n", GetLastError());CryptDestroyKey(hKey);CryptReleaseContext(hProv, 0);return "";}// 释放资源CryptDestroyKey(hKey);CryptReleaseContext(hProv, 0);// 将解密后的字节数组转换为字符串return std::string(decryptedBytes.begin(), decryptedBytes.begin() + dataLen);
}
例子如下
std::string key = "1frz2wsx";
std::string iv = "8uhb7yxc";int Test()
{//加密std::string plainText = "awsxedc";std::string encryptedData = DESEncrypt(plainText, key, iv);if (!encryptedData.empty()) {std::cout << "Encrypted data (Base64): " << encryptedData << std::endl;}//解密std::string decryptedData = DESDecrypt(encryptedData, key, iv);if (!decryptedData.empty()) {std::cout << "Decrypted Data: " << decryptedData << std::endl;}return 0;
}