Windows XP 中 使用 CryptoApi 解密成功,但解密出来的数据是错误的
Windows XP 中 使用 CryptoApi 解密成功,但解密出来的数据是错误的。
使用的算法是 DES 和 RC4,解密时调用的每个 Api 都检查了返回值,都是正常返回。
什么原因,同一个程序,在绝大部分机器上都是正确的,
但在个别(约1%)的机器上解密出来的数据是错误的。
有哪位大侠知道原因?
程序如下:
HCRYPTKEY hKey = 0;
HCRYPTHASH hHash = 0;
DWORD errCode = NO_ERROR;
DWORD dwCount = nLength;
DWORD dwKeyLength;
const BYTE* pKeyData = GetDecryptKey(&dwKeyLength);
LPCSTR pErrMsg = 0;
if (0 == pErrMsg && !CryptCreateHash(m_hProv, CALG_SHA, 0, 0, &hHash))
pErrMsg = _VT("Error when CreateHash");
if (0 == pErrMsg && !CryptHashData(hHash, (BYTE*)pKeyData, dwKeyLength, 0))
pErrMsg = _VT("Error when HashData");
if (0 == pErrMsg && !CryptDeriveKey(m_hProv, m_algDecrypt, hHash, 0, &hKey))
pErrMsg = _VT("Error when DeriveKey from hash");
if (0 == pErrMsg && !CryptDecrypt(hKey, 0, FALSE, 0, (PBYTE)pBuffer, &dwCount))
pErrMsg = _VT("Error when Decrypt");
if (!hKey) CryptDestroyKey(hKey);
if (!hHash) CryptDestroyHash(hHash);
if (pErrMsg)
{
ErrMessageBox(vGetLastError(), pErrMsg);
Exit(-4);
}
return TRUE;