为什么我的 CryptDecrypt 失败并显示错误代码 0x80090005 (NTE_BAD_DATA)?

发布于 2024-12-18 00:05:22 字数 1692 浏览 0 评论 0原文

我编写了这段代码来使用给定的密钥和 iv 解密给定的密码。它工作正常,直到 CryptDecrypt 返回 false 并且 GetLastError() 返回 0x80090005 (应该是 NTE_BAD_DATA)。

#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;
}

如果您有任何想法,请分享:)

提前致谢...

I wrote this piece of code to decrypt a given cipher using a given key and iv. It works fine until CryptDecrypt which returns false and GetLastError() returns 0x80090005 (which should be NTE_BAD_DATA).

#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;
}

If you have any idea, please share :)

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文