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退出的進程按任意鍵繼續。 。 。
請詢問這裏之前先調試程序。 –