0
你能帮我这个代码吗?调用函数时会挂起。程序挂起在函数调用Wincrypt函数
#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;
}
调用函数时,代码崩溃,我想我通过传递指针来做错事情。
扔“字符常量*”
的实例,此应用程序已请求运行时终止它在 不寻常的方式终止后调用。请联系应用程序的支持团队获取更多 信息。
--------------------------------用返回值255退出的进程按任意键继续。 。 。
请询问这里之前先调试程序。 –