gpt4 book ai didi

c++ - CryptoAPI RSA : CryptDecrypt decrypts only at the first time, 其他调用返回 NTE_BAD_DATA

转载 作者:行者123 更新时间:2023-11-30 05:38:07 26 4
gpt4 key购买 nike

我已经编写了加密/解密自定义长度内存缓冲区的程序。加密结束很好;但是我的解密代码在任何缓冲区位置只解密一次数据,对应于 block 条目。其他 block 的解密以 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;
}

最佳答案

RSA 不应该以这种方式使用。它真的不是 block 密码(或流密码,就此而言)。据我了解,除了一条“短”消息外,它实际上没有任何密码学用途,所以我对库在一次 key 解密后失败并不感到惊讶。

如果您需要保护任意大小的数据,请使用 RSA 将对称 key 交换为流或 block 密码(如 AES)。

关于c++ - CryptoAPI RSA : CryptDecrypt decrypts only at the first time, 其他调用返回 NTE_BAD_DATA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32933411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com