我使用 Microsoft CryptoAPI 编写了一些代码来计算 SHA-1,并让编译后的 exe 在 Windows 7、Win Server 2008、Win Server 2003 上运行。但是,当我在 Windows XP SP3 下运行它时,它不起作用。
我将失败的范围缩小到CryptAcquireContext()
通话。
我确实注意到之前的一篇文章谈到了“ ……(原型) ”的 XP 错误命名,它必须通过使用 WinXP 特定的宏 MS_ENH_RSA_AES_PROV_XP 来解决。
我做了XP特定的代码修改,它仍然不起作用。(bResult
在 Win XP 上返回 0 false,所有其他平台bResult
返回 1 true。)
我用我在 regedit.exe 中看到的实际键+字符串值检查了 MS_ENH_RSA_AES_PROV_XP,所以一切看起来都可以正常工作但没有成功。
我是否忽略了一些使它在 Windows XP 上工作的东西?
我已经粘贴了最短的例子来说明这个问题。我使用了 VS2010 C++。
// based on examples from http://msdn.microsoft.com/en-us/library/ms867086.aspx
#include "windows.h"
#include "wincrypt.h"
#include <iostream>
#include <iomanip> // for setw()
void main()
{
BOOL bResult;
HCRYPTPROV hProv;
// Attempt to acquire a handle to the default key container.
bResult = CryptAcquireContext(
&hProv, // Variable to hold returned handle.
NULL, // Use default key container.
MS_DEF_PROV, // Use default CSP.
PROV_RSA_FULL, // Type of provider to acquire.
0); // No special action.
std::cout << "line: " << std::setw(4) << __LINE__ << "; " << "bResult = " << bResult << std::endl;
if (! bResult) { // try Windows XP provider name
bResult = CryptAcquireContext(
&hProv, // Variable to hold returned handle.
NULL, // Use default key container.
MS_ENH_RSA_AES_PROV_XP, // Windows XP specific instead of using default CSP.
PROV_RSA_AES, // Type of provider to acquire.
0); // No special action.
std::cout << "line: " << std::setw(4) << __LINE__ << "; " << "bResult = " << bResult << std::endl;
}
if (bResult)
CryptReleaseContext(hProv, 0);
}
Windows 7 成功:
Windows XP 故障: