我正在尝试使用128BIT AES解密来解密一些东西。当我尝试调用CryptDecrypt时,我得到一个错误,声明“指定的算法无效”。我在使用这里发布的库时也遇到了同样的问题:http://www.codeproject.com/KB/security/WinAES.aspx
导致此错误的原因是什么?
我在visual Studio2008的vista64bit上使用CryptoAPI。我检查了注册表,AES库就在那里...
编辑
BYTE*& encryptedData /* get data length */
HCRYPTPROV cryptoHandle = NULL;
HCRYPTKEY aesKeyHandle = NULL;
hr = InitWinCrypt(cryptoHandle);
if(FAILED(hr))
{
return hr;
}
AesKeyOffering aesKey = { {PLAINTEXTKEYBLOB, CUR_BLOB_VERSION, 0, CALG_AES_128}, 16, { 0xFF, 0x00, 0xFF, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4, 0x04 }};
if(CryptImportKey(cryptoHandle, (CONST BYTE*)&aesKey, sizeof(AesKeyOffering), NULL, 0, &aesKeyHandle) == FALSE)
{
// DO error
return HRESULT_FROM_WIN32(GetLastError());
}
if(CryptSetKeyParam(aesKeyHandle, KP_IV, { 0xFF, 0x00, 0xFF, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4, 0x04 } , 0) == FALSE)
{
return HRESULT_FROM_WIN32(GetLastError());
}
BYTE blah2 = CRYPT_MODE_CBC;
// set block mode
if(CryptSetKeyParam(aesKeyHandle, KP_MODE, &blah2, 0) == FALSE)
{
//
return HRESULT_FROM_WIN32(GetLastError());
}
DWORD lol = dataLength / 16 + 1;
DWORD lol2 = lol * 16;
if(CryptDecrypt(aesKeyHandle, 0, TRUE, 0, encryptedData, &lol2) == FALSE)
{
return HRESULT_FROM_WIN32(GetLastError());
}InitWinCrypt函数
if(!CryptAcquireContextW(&cryptoHandle, NULL, L"Microsoft Enhanced RSA and AES Cryptographic Provider", PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
{
if(!CryptAcquireContextW(&cryptoHandle, NULL, L"Microsoft Enhanced RSA and AES Cryptographic Provider", PROV_RSA_AES, 0))
{
return HRESULT_FROM_WIN32(GetLastError());
}
else
{
return S_OK;
}
}
return S_OK;AesOffering结构:
struct AesKeyOffering
{
BLOBHEADER m_Header;
DWORD m_KeyLength;
BYTE Key[16];
};EDIT2
在重新启动我的计算机,并重新调用CBC块之后。我现在收到坏数据错误。在C#中可以很好地解密数据。但我需要使用wincrypt来完成此操作。
发布于 2009-10-07 11:10:22
您是否通过引用InitWithCrypt来传递cryptoHandle?如果不是,您的代码
if(!CryptAcquireContextW(&cryptoHandle, ...只会修改InitWinCrypt的cryptoHandle副本。
EDIT:确实如此,试着去掉设置CRYPT_MODE_CBC的CryptSetKeyParam调用
https://stackoverflow.com/questions/1529208
复制相似问题