Algoritmo inválido especificado Cryptoapi
Pergunta
Estou tentando descriptografar algo usando descriptografia de AES de 128 bits. Quando tento chamar o CryptDecrypt, recebo um erro indicando "o algoritmo inválido especificado". Tenho o mesmo problema ao usar a biblioteca postada aqui: http://www.codeproject.com/kb/security/winaes.aspx
O que pode causar esse erro?
Estou usando o Cryptoapi junto no Vista64bit com o Visual Studio 2008. Verifiquei no registro e a Biblioteca da AES está lá ...
EDITAR
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());
}
Função 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;
Estrutura aesofferante:
struct AesKeyOffering
{
BLOBHEADER m_Header;
DWORD m_KeyLength;
BYTE Key[16];
};
Edit2
Depois de reiniciar meu computador e reformar o pedaço da CBC. Agora estou recebendo erros de dados ruins. Os dados descriptografam bem em C#. Mas eu preciso fazer isso usando o Wincrypt.
Solução
Você está passando cryptoHandle
por referência a InitWithCrypt
? Se não, seu código
if(!CryptAcquireContextW(&cryptoHandle, ...
apenas modificaria InitWinCrypt
cópia de cryptoHandle
.
EDITAR: Dado que sim, tente se livrar do CryptSetKeyParam
Ligue para quais define CRYPT_MODE_CBC