你能帮我解决这个问题吗?它在调用函数AesDecrypt
时挂起。
#include <iostream>
#include <Windows.h>
#include <wincrypt.h>
struct AesKey {
BLOBHEADER Header;
DWORD dwKeyLength;
BYTE cbKey[16];
AesKey() {
ZeroMemory(this, sizeof(*this));
Header.bType = PLAINTEXTKEYBLOB;
Header.bVersion = CUR_BLOB_VERSION;
Header.reserved = 0;
Header.aiKeyAlg = CALG_AES_128;
dwKeyLength = 16;
}
};
void AesDecrypt(unsigned char *output, unsigned char *input, int inLen, unsigned char *key, unsigned char *iv, int &plainSize) {
HCRYPTPROV provider;
AesKey rawKey;
HCRYPTKEY cKey;
BOOL hr = CryptAcquireContext(&provider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0);
if (hr == FALSE)
throw "Unable to acquire AES Context";
for (int i = 0; i < 16; i++)
rawKey.cbKey[i] = key[i];
hr = CryptImportKey(provider, (BYTE *) &rawKey, sizeof(AesKey), NULL, 0, &cKey);
if (hr == FALSE)
throw "Unable to import given key";
hr = CryptSetKeyParam(cKey, KP_IV, (BYTE *) iv, 0);
if (hr == FALSE)
throw "Unable to set IV";
DWORD dwMode = CRYPT_MODE_CBC;
hr = CryptSetKeyParam(cKey, KP_MODE, (BYTE*) &dwMode, 0);
if (hr == FALSE)
throw "Unable to set mode";
memcpy(output, input, inLen);
DWORD d = (DWORD) inLen;
hr = CryptDecrypt(cKey, NULL, TRUE, 0, output, &d);
if (hr == FALSE)
{
int err = GetLastError();
throw "Error during Decryption";
}
plainSize = d;
}
// codigo generado por mi
int main(int argc, char** argv) {
unsigned char input[] = "SE83loTjmMeaG9+xoIyjng==";
unsigned char *pinput = input;
unsigned char output[] = "SE83loTjmMeaG9+xoIyjng==";
unsigned char *poutput = output;
unsigned char iv[] = "1234567890ABCDEF";
unsigned char *piv = iv;
unsigned char key[] = "1234567890ABCDEF";
unsigned char *pkey = key;
int plaInt;
AesDecrypt(poutput, pinput, 24, pkey, piv, plaInt);
std::cout << output << std::endl;
return 0;
}
调用函数时代码崩溃,我认为通过传递指针我做错了。
在抛出'char const *'
的实例后终止调用此应用程序已请求运行时将其终止 不寻常的方式请联系应用程序的支持团队获取更多信息 信息。
--------------------------------进程退出并返回值255按任意键继续。 。
答案 0 :(得分:1)
我怀疑这条线是罪魁祸首。
hr = CryptImportKey(provider, (BYTE *) &rawKey, sizeof(AesKey), NULL, 0, &cKey);
您可能打算使用
hr = CryptImportKey(provider, rawKey.cbKey, sizeof(AesKey), NULL, 0, &cKey);