CryptoAPI RSA:CryptDecrypt仅第一次解密,其他调用返回NTE_BAD_DATA

时间:2015-10-04 12:22:44

标签: c++ encryption cryptoapi

我已经编写了加密/解密自定义长度的内存缓冲区的程序。 Ecryption结束很好;但我的解密代码只在一个缓冲区位置解密一次数据,对应于块条目。其他块的解密以NTE_BAD_DATA结束。

你有什么建议为什么会这样?

这是我的加密代码:

void CWinRSA::FinishEncrypt(const char* pcbRawData, const size_t nDataSize, char** ppcbEcrData, size_t& rnEcrSize) const
{
    if (m_hProvider == NULL)
    {
        throw ("Cannot encrypt data with wrong provider!!");
    }

    if (m_hKey == NULL)
    {
        throw ("Cannot encrypt data with a wrong key!!");
    }

    size_t nBlockLength = GetBlockLength();
    size_t nPaddingSize = nBlockLength - 11;

    size_t nRemain = nDataSize % nBlockLength;
    size_t nBlockProcess = (nDataSize / nPaddingSize + (nRemain != 0 ? 1 : 0));

    size_t nResultSize = nBlockProcess * nBlockLength;

    (*ppcbEcrData) = new char[nResultSize];

    DWORD dwBufferLength = nBlockLength;
    DWORD dwDataLength;

    for (int iBlock = 0; iBlock < nBlockProcess - 1; iBlock++)
    {
        memcpy((*ppcbEcrData) + (iBlock * nBlockLength),
            pcbRawData + (iBlock * nPaddingSize), nPaddingSize);

        dwDataLength = nPaddingSize;

        if (!CryptEncrypt(m_hKey, NULL, FALSE, 0,
            (BYTE*)((*ppcbEcrData) + (iBlock * nBlockLength)),
            &dwDataLength, dwBufferLength))
        {
            throw ("Cannot encrypt data!!");
        }
    }

    memcpy((*ppcbEcrData) + ((nBlockProcess - 1) * nBlockLength),
        pcbRawData + ((nBlockProcess - 1) * nPaddingSize), (nRemain ? nRemain : nPaddingSize));

    dwDataLength = (nRemain ? nRemain : nPaddingSize);

    if (!CryptEncrypt(m_hKey, NULL, TRUE, 0,
        (BYTE*)((*ppcbEcrData) + ((nBlockProcess - 1) * nBlockLength)),
        &dwDataLength, dwBufferLength))
    {
        throw ("Cannot encrypt data!!");
    }

    rnEcrSize = nResultSize;
}

解密:

void CWinRSA::FinishDecrypt(const char* pcbRawData, const size_t nDataSize, char** ppcbDecData, size_t& rnDecSize) const
{
    if (m_hProvider == NULL)
    {
        throw ("Cannot decrypt data with wrong provider!!");
    }

    if (m_hKey == NULL)
    {
        throw ("Cannot decrypt data with a wrong key!!");
    }

    size_t nBlockLength = GetBlockLength();

    if ((nDataSize % nBlockLength) != 0)
    {
        throw ("Cannot decrypt data!! Probably data is corrupted!!");
    }

    size_t nPaddingSize = nBlockLength - 11;
    size_t nBlockProcess = nDataSize / nBlockLength;

    size_t nResultSize = nBlockProcess * nPaddingSize;

    (*ppcbDecData) = new char[nResultSize];

    DWORD dwDataLength;

    char* pcbComputeResult = new char[nBlockLength];

    for (int iBlock = 0; iBlock < nBlockProcess - 1; iBlock++)
    {
        memcpy(pcbComputeResult, pcbRawData + (iBlock * nBlockLength), nBlockLength);

        if (!CryptDecrypt(m_hKey, NULL, FALSE, 0, (BYTE*)pcbComputeResult, &dwDataLength))
        {
            throw ("Cannot decrypt data!!");
        }

        memcpy((*ppcbDecData) + (iBlock * nPaddingSize), pcbComputeResult, nPaddingSize);
    }

    memcpy(pcbComputeResult, pcbRawData + ((nBlockProcess - 1) * nBlockLength), nBlockLength);

    if (!CryptDecrypt(m_hKey, NULL, TRUE, 0, (BYTE*)pcbComputeResult, &dwDataLength))
    {
        DWORD dwError = GetLastError();

        throw ("Cannot decrypt data!!");
    }

    memcpy((*ppcbDecData) + ((nBlockProcess - 1) * nPaddingSize), pcbComputeResult, nPaddingSize);

    rnDecSize = ((nBlockProcess - 1) * nPaddingSize) + dwDataLength;

    delete[] pcbComputeResult;
    pcbComputeResult = NULL;
}

2 个答案:

答案 0 :(得分:1)

RSA不应该以这种方式使用。它实际上不是分组密码(或者是流密码,就此而言)。我的理解是除了单个“短”消息之外它确实没有任何密码学用途,因此在从密钥解密后库失败并不令我感到惊讶。

如果需要保护任意大小的数据,请使用RSA交换流或分组密码的对称密钥(如AES)。

答案 1 :(得分:0)

我找到了答案。在所有解密调用之前,我必须用块长度(以字节为单位)初始化dwDataLength。

dwDataLength = nBlockLength;

if (!CryptDecrypt(m_hKey, NULL, TRUE, 0, (BYTE*)pcbComputeResult, &dwDataLength))
{
     DWORD dwError = GetLastError();

     throw ("Cannot decrypt data!!");
}

整个解密方法

void CWinRSA::FinishDecrypt(const char* pcbRawData, const size_t nDataSize, char** ppcbDecData, size_t& rnDecSize) const
{
    if (m_hProvider == NULL)
    {
        throw ("Cannot decrypt data with wrong provider!!");
    }

    if (m_hKey == NULL)
    {
        throw ("Cannot decrypt data with a wrong key!!");
    }

    size_t nBlockLength = GetBlockLength();

    if ((nDataSize % nBlockLength) != 0)
    {
        throw ("Cannot decrypt data!! Probably data is corrupted!!");
    }

    size_t nPaddingSize = nBlockLength - 11;
    size_t nBlockProcess = nDataSize / nBlockLength;

    size_t nResultSize = nBlockProcess * nPaddingSize;

    (*ppcbDecData) = new char[nResultSize];

    DWORD dwDataLength;

    char* pcbComputeResult = new char[nBlockLength];

    for (int iBlock = 0; iBlock < nBlockProcess - 1; iBlock++)
    {
        memcpy(pcbComputeResult, pcbRawData + (iBlock * nBlockLength), nBlockLength);

        dwDataLength = nBlockLength;

        if (!CryptDecrypt(m_hKey, NULL, FALSE, 0, (BYTE*)pcbComputeResult, &dwDataLength))
        {
            throw ("Cannot decrypt data!!");
        }

        memcpy((*ppcbDecData) + (iBlock * nPaddingSize), pcbComputeResult, nPaddingSize);
    }

    memcpy(pcbComputeResult, pcbRawData + ((nBlockProcess - 1) * nBlockLength), nBlockLength);

    dwDataLength = nBlockLength;

    if (!CryptDecrypt(m_hKey, NULL, TRUE, 0, (BYTE*)pcbComputeResult, &dwDataLength))
    {
        throw ("Cannot decrypt data!!");
    }

    memcpy((*ppcbDecData) + ((nBlockProcess - 1) * nPaddingSize), pcbComputeResult, nPaddingSize);

    rnDecSize = ((nBlockProcess - 1) * nPaddingSize) + dwDataLength;

    delete[] pcbComputeResult;
    pcbComputeResult = NULL;
}