3

我使用 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 故障: 在此处输入图像描述

4

1 回答 1

5

在您的 CryptAcquireContext 代码中,您似乎缺少获取没有特定容器集的上下文的参数。您需要在 CryptAcquireContext 中传递 CRYPT_VERIFYCONTEXT 选项。

Windows 7 可能正在解决这个问题。

http://msdn.microsoft.com/en-us/library/windows/desktop/aa379886(v=vs.85).aspx

为了进一步诊断,GetLastError() 的结果将是必需的。

于 2012-03-20T20:58:23.007 回答