C++ CryptoAPI RSA:CryptoDecrypt仅在第一次解密时解密,其他调用返回NTE_BAD_数据
我已经写了程序加密/解密自定义长度的内存缓冲区。亵渎结束得很好;但我的解密代码在任何缓冲区位置只对数据解密一次,它对应于块条目。其他块的解密以NTE_BAD_数据结束 你有什么建议为什么会这样 这是我的加密代码:C++ CryptoAPI RSA:CryptoDecrypt仅在第一次解密时解密,其他调用返回NTE_BAD_数据,c++,encryption,cryptoapi,C++,Encryption,Cryptoapi,我已经写了程序加密/解密自定义长度的内存缓冲区。亵渎结束得很好;但我的解密代码在任何缓冲区位置只对数据解密一次,它对应于块条目。其他块的解密以NTE_BAD_数据结束 你有什么建议为什么会这样 这是我的加密代码: void CWinRSA::FinishEncrypt(const char* pcbRawData, const size_t nDataSize, char** ppcbEcrData, size_t& rnEcrSize) const { if (m_hProvi
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::FinishEncrypt(常量字符*pcbRawData,常量大小数据大小,字符**ppcbEcrData,大小数据大小&rnEcrSize)常量
{
if(m_hProvider==NULL)
{
抛出(“无法使用错误的提供程序加密数据!!”;
}
如果(m_hKey==NULL)
{
抛出(“不能用错误的密钥加密数据!!”;
}
size_t nBlockLength=GetBlockLength();
大小\u t nPaddingSize=nBlockLength-11;
大小\u t nRemain=n缓冲%n块长度;
大小\u t nBlockProcess=(nDasize/nPaddingSize+(nRemain!=0?1:0));
大小\u t nResultSize=nBlockProcess*nBlockLength;
(*ppcbEcrData)=新字符[nResultSize];
DWORD dwBufferLength=nBlockLength;
DWORD数据长度;
对于(int-iBlock=0;iBlock
解密:
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;
}
void CWinRSA::FinishDecrypt(const char*pcbRawData、const size\u t ndasize、char**ppcbDecData、size\u t&rnDecSize)const
{
if(m_hProvider==NULL)
{
抛出(“无法使用错误的提供程序解密数据!!”;
}
如果(m_hKey==NULL)
{
抛出(“不能用错误的密钥解密数据!!”;
}
size_t nBlockLength=GetBlockLength();
如果((nDasize%nBlockLength)!=0)
{
抛出(“无法解密数据!!数据可能已损坏!!”;
}
大小\u t nPaddingSize=nBlockLength-11;
尺寸\u t nBlockProcess=N底座/nBlockLength;
大小\u t nResultSize=nBlockProcess*nPaddingSize;
(*ppcbDecData)=新字符[nResultSize];
DWORD数据长度;
char*pcbComputeResult=新字符[nBlockLength];
对于(int-iBlock=0;iBlock
RSA不应该这样使用。它实际上不是分组密码(或流密码)。我的理解是,除了一条“短”消息之外,它实际上没有任何密码学用途,因此,当从密钥解密一次后库失败时,我并不感到惊讶
如果需要保护任意大小的数据,请使用RSA将对称密钥交换为流密码或分组密码(如AES)。我找到了答案。在所有解密调用之前,我必须使用块长度(以字节为单位)初始化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;
}
void CWinRSA::FinishDecrypt(const char*pcbRawData、const size\u t ndasize、char**ppcbDecData、size\u t&rnDecSize)const
{
if(m_hProvider==NULL)
{
抛出(“无法使用错误的提供程序解密数据!!”;
}
如果(m_hKey==NULL)
{
抛出(“不能用错误的密钥解密数据!!”;
}
size_t nBlockLength=GetBlockLength();
如果((nDasize%nBlockLength)!=0)
{
抛出(“无法解密数据!!数据可能已损坏!!”;
}
大小\u t nPaddingSize=nBlockLength-11;
尺寸\u t nBlockProcess=N底座/nBlockLength;
大小\u t nResultSize=nBlockProcess*nPaddingSize;
(*ppcbDecData)=新字符[nResultSize];
DWORD数据长度;
char*pcbComputeResult=新字符[nBlockLength];
对于(int-iBlock=0;iBlock