【发布时间】:2018-07-11 20:03:15
【问题描述】:
我正在尝试使用 CryptoAPI 加密 AES128 中的字符串,但由于某种原因我没有得到它。当我编译时,它只会使应用程序崩溃。我相信问题出在CryptDecrypt 和CryptEncrypt 函数的调用中,因为我是根据我在互联网上找到的另一个函数编写这个函数的。我对它一无所知,也不知道如何使用它。
这是我的代码:
#include <Windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <cmath>
#include <string>
#pragma comment(lib, "crypt32.lib")
#define BLOCK_LEN 16
std::string AES128(std::string key, std::string data, bool enc)
{
bool Result = false;
size_t blocks = ceil((float)data.length() / BLOCK_LEN) + 1;
BYTE* chunk = new BYTE[blocks * BLOCK_LEN];
memset(chunk, 0, blocks * BLOCK_LEN);
memcpy(chunk, data.c_str(), data.length());
HCRYPTPROV hProv;
if (!CryptAcquireContextA(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
goto finally;
HCRYPTHASH hHash;
if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash))
goto finally;
if (!CryptHashData(hHash, (BYTE*)key.c_str(), key.length(), 0))
goto finally;
HCRYPTKEY hKey;
if (!CryptDeriveKey(hProv, CALG_AES_128, hHash, 0, &hKey))
goto finally;
for (int i = 0; i < blocks; i++)
switch (enc)
{
case true:
{
DWORD out_len = BLOCK_LEN;
if (!CryptEncrypt(hKey, NULL, i + 1 == blocks, NULL, &chunk[i * BLOCK_LEN], &out_len, blocks * BLOCK_LEN))
goto finally;
break;
}
case false:
{
DWORD out_len = BLOCK_LEN;
if (!CryptDecrypt(hKey, NULL, i + 1 == blocks, NULL, &chunk[i * BLOCK_LEN], &out_len))
goto finally;
break;
}
}
Result = true;
goto finally;
finally:
{
if (hProv)
CryptReleaseContext(hProv, 0);
if (hHash)
CryptDestroyHash(hHash);
if (hKey)
CryptDestroyKey(hKey);
if (Result)
return std::string(reinterpret_cast<char*>(chunk));
else
return "";
}
}
int main()
{
std::string key = "12345";
std::string data = "aaaaaabbbbbb";
std::string encdata = AES128(key, data, true);
std::string decdata = AES128(key, encdata, false);
printf("%s => %s => %s", data.c_str(), encdata.c_str(), decdata.c_str());
system("pause");
}
抱歉英语不好,我是巴西人。
【问题讨论】:
-
编译时会崩溃吗?
-
不,当我使用调试器运行程序时它崩溃了。
-
如果葡萄牙语对你来说更方便,别忘了还有Stack Overflow em Português。
-
什么是崩溃?什么线路,什么信号,什么堆栈跟踪?