CryptGenKey fails with Access Denied

From: gP T Gr8 (gprasadholla_at_gmail.com)
Date: 09/19/05


Date: 19 Sep 2005 09:58:48 -0700

Hi i have a problem using CryptGenKey.
Here is the part of code where its failing..
        // Acquire context for RSA key
        fResult = CryptAcquireContext(&hProv,
                CONTAINERNAME,
                CSP_PROVIDER,
                CSP_PROVIDER_TYPE,
                CRYPT_MACHINE_KEYSET);
                if (!fResult)
                {
                // Create a key container if one does not exist.
                fResult = CryptAcquireContext(&hProv,
                        CONTAINERNAME,
                        CSP_PROVIDER,
                        CSP_PROVIDER_TYPE,
                        CRYPT_NEWKEYSET|CRYPT_MACHINE_KEYSET);
                if (!fResult)
                        {
                                break;
                        }
                        }

                // get the RSA key handle
                fResult = CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hRSAKey);
                if (!fResult)
                {
                        if (GetLastError() == NTE_NO_KEY)
                        {
                                // Create a key if one does not exist.
                                fResult = CryptGenKey(hProv,
                                        AT_KEYEXCHANGE,
                                        CRYPT_EXPORTABLE,
                                        &hRSAKey);
                                if (!fResult)
                                {
                                        dwErr = GetLastError(); //here im getting the error
                                        break;
                                }
                        }
                        else
                        {
                        dwErr = GetLastError();
                        break;
                        }
                        }

Im using machine key set as i want to share session key accross users
in the machine. And i am deleting it while un installation time.
The same code works fine in my machine, but in some machines it fails
with error code 5, and in some machines where it used to work some time
back, is giving NTE_FAIL.
Am i doing some thing wrong here.
Pls let me know, if u have any clue about this
Thanx