1

我正在尝试使用 128BIT AES 解密来解密某些东西。当我尝试调用 CryptDecrypt 时,我收到一条错误消息,指出“指定的算法无效”。使用此处发布的库时遇到同样的问题:http: //www.codeproject.com/KB/security/WinAES.aspx

什么会导致此错误?

我在 vista64bit 和 Visual Studio 2008 上使用 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];
};

编辑2

重新启动我的计算机并删除 CBC 块后。我现在收到错误数据错误。数据在 C# 中可以很好地解密。但我需要使用wincrypt来做到这一点。

4

1 回答 1

1

你是cryptoHandle通过引用传递的InitWithCrypt吗?如果没有,你的代码

if(!CryptAcquireContextW(&cryptoHandle, ...

只会InitWinCrypt修改cryptoHandle.


编辑:鉴于确实如此,请尝试摆脱设置的CryptSetKeyParam呼叫CRYPT_MODE_CBC

于 2009-10-07T03:10:22.133 回答