MS CryptoAPI doesn't work on Windows XP with CryptAcquireContext ()

I wrote the code using Microsoft CryptoAPI to compute SHA-1 and got the compiled exe working on Windows 7, Win Server 2008, Win Server 2003. However, when I run it under Windows XP SP3, it doesn't work.

I have narrowed down the challenge CryptAcquireContext()

.

I noticed that the previous post talked about the wrong XP naming "... (Prototype)" and it should be accounted for with the special WinXP macro MS_ENH_RSA_AES_PROV_XP.

I made a modification to the XP code and it still doesn't work. ( bResult

Returns 0 false on Win XP; all other platforms bResult

return 1 true.)

I checked MS_ENH_RSA_AES_PROV_XP with the actual key + string values ​​that I see in regedit.exe, so everything looks like it works but fails.

Am I missing something to get it to work on Windows XP?

I have provided the shortest possible example to illustrate the problem. I used 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: enter image description here

Windows XP error: enter image description here

+3


source to share


1 answer


In the CryptAcquireContext code, it seems you are missing a parameter to get the context without a specific set of containers. You need to pass CRYPT_VERIFYCONTEXT parameter to CryptAcquireContext.

Windows 7 can work around this.



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

For further diagnostics, the results of GetLastError () will be needed.

+5


source







All Articles