tags:

views:

354

answers:

1

I am trying to decrypt something using 128BIT AES Decryption. When i attempt to calling CryptDecrypt i get an Error stating "Invalid Algorithm Specified". I get the same problem when using the library posted here: http://www.codeproject.com/KB/security/WinAES.aspx

What can cause this error?

I am using CryptoAPI along on vista64bit with visual studio 2008. I checked in the registry and the AES library is there...

EDIT

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 function

    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:

struct AesKeyOffering
{
    BLOBHEADER m_Header;
    DWORD m_KeyLength;
    BYTE Key[16];
};

EDIT2

After rebooting my computer, and remvoing the CBC chunk. I am now getting Bad Data Errors. The data decrypts fine in C#. But i need to do this using wincrypt.

+1  A: 

Are you passing cryptoHandle by reference to InitWithCrypt? If not, your code

if(!CryptAcquireContextW(&cryptoHandle, ...

would only modify InitWinCrypt's copy of cryptoHandle.


EDIT: Given that it does, try getting rid of the CryptSetKeyParam call which sets CRYPT_MODE_CBC

GRB
UberJumper
Did that and same error, i've tried like every single possible thing.
UberJumper
odd, I was also getting "invalid algorithm specified" with your code until I made that change (32-bit vista, msvc 2008)
GRB
well here's a question, for your `KP_IV` call, how are you able to get `{ 0xFF, ... }` to compile? I would replace that with `aesKey.Key`
GRB
I actually do use aesKey.key for that i just screwed uip that part slightly.When changing the key around.
UberJumper
Okay so after rebooting my PC i now get BAD Data. I am so confused.
UberJumper
That's a good sign, in that it means it should work now (you get a bad data error because you're presumably not giving it actual data to decrypt). Try and `CryptEncrypt` some data, then pass it to `CryptDecrypt`
GRB
I already have a small C# application that encrypts data. I can decrypt the data using a program, fine. =(
UberJumper
I'm sorry, what are you saying? You pass the data from the C# program yet it gives the bad data error?
GRB
Sorry ive been up for like 32 hours straight now. Basically i wrote a small C# application to encrypt random files. So no matter what i pass to it. It appears to be failing and saying bad data. I also noticed something odd. I quickly wrote something that decrypted RC2 using wincrypt. It decrypts fine. But DecryptData still returns FALSE. Why could that be?
UberJumper
Does `GetLastError()` tell you anything?
GRB
Bad Data. Is what it always returns. It appears to decrypt but stops almost near the end and just spits out bad data.
UberJumper