我使用 Microsoft CryptoAPI 编写了一些代码来计算 SHA-1,并让编译后的 exe 在 Windows 7、Win Server 2008、Win Server 2003 上运行。但是,当我在 Windows XP SP3 下运行它时,它不起作用。
我将失败的范围缩小到CryptAcquireContext()
call.
我确实注意到了以前的帖子 https://stackoverflow.com/a/4523302谈到 XP 的错误命名“… (原型)" 并且必须通过使用 WinXP 特定宏 MS_ENH_RSA_AES_PROV_XP 来解决。
我做了XP特定的代码修改,但它仍然不起作用。 (这bResult
在 Win XP 和所有其他平台上返回 0 falsebResult
返回 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 success:
Windows XP failure: