We help IT Professionals succeed at work.
Get Started

CryptSetKeyParam - Invalid Parameter (Urgent, please help)

ashrobo
ashrobo asked
on
2,213 Views
Last Modified: 2007-12-19
Hi,
I am trying to to generate a random key by following an example from MSDN, and I am unable to do so. Below is the code that I have adapted to my program. CryptAcquireContext is commented because I have acquired the context before that. My logs shows that CryptSetKeyParam was not successful hence unable to get a random key.

unsigned long Crypt::GenMasterKey() {
   DWORD dwMode;
    BYTE pbData[16];
    DWORD dwCount;
    DWORD i;
    LPCSTR containerName = "MyKeyContainer";
   
    Log("\t\tGenMasterKey.........");

    // Get a handle to the user default provider.
    //if(!CryptAcquireContext(&m_hProv, containerName, MS_DEF_PROV, PROV_RSA_FULL, 0))
    //    Log("GenMasterKey..Error during CryptAcquireContext!");
    //else
      if (m_hProv != 0) {
        if (!CryptCreateHash(m_hProv,   // handle to CSP
                  CALG_SHA,  // hashing algorithm
                  0,         // non-keyed hash
                  0,         // reserved, must be zero
                  &m_hKey)) { // hash object
            if(GetLastError() == ERROR_INVALID_HANDLE)
                Log("Error CryptGenKey.. Invalid handle");
            if(GetLastError() == ERROR_INVALID_PARAMETER)    
                Log("Error CryptGenKey.. Invalid parameter");
            if(GetLastError() == NTE_BAD_ALGID)
                Log("Error CryptGenKey.. NTE_BAD_ALGID");
            if(GetLastError() == NTE_BAD_FLAGS)
                Log("Error CryptGenKey..NTE_BAD_FLAGS");
            if(GetLastError() == NTE_BAD_UID)
                Log("Error CryptGenKey.. NTE_BAD_UID");
           
            Log("GenMasterKey..Error during CryptGenKey!");
        } else {
                  // Set the cipher mode.
                  dwMode = CRYPT_MODE_ECB;

            if(!CryptSetKeyParam(m_hKey,                // <---------- error occurs here
                KP_MODE,
                (unsigned char*) &dwMode,                // <-------------- it was original just &dwMode, and I added (unsigned char*) because it cant                                                                           // compile without that
                0)) {
            if(GetLastError() == ERROR_INVALID_HANDLE)
                   Log("Error CryptGenKey.. Invalid handle");
            if(GetLastError() == ERROR_INVALID_PARAMETER)
                Log("Error CryptGenKey.. Invalid parameter");//
            if(GetLastError() == ERROR_BUSY)
                Log("Error CryptGenKey.. ERROR_BUSY");
            if(GetLastError() == NTE_BAD_FLAGS)
                Log("Error CryptGenKey..NTE_BAD_FLAGS");
            if(GetLastError() == NTE_BAD_UID)
                Log("Error CryptGenKey.. NTE_BAD_UID");
            if(GetLastError() == NTE_FAIL)
                Log("Error CryptGenKey.. NTE_FAIL");

                        Log("GenMasterKey..Error during CryptSetKeyParam!");
            } else if(!CryptGenRandom(m_hProv, 8, pbData))    // Generate a random initialization vector.
                        Log("GenMasterKey..Error during CryptGenRandom!");
                  else if(!CryptSetKeyParam(m_hKey, KP_IV, pbData, 0))  // Set the initialization vector.
                        Log("GenMasterKey..Error during CryptSetKeyParam!");
                  else {
                        return m_hKey;
                  }
            }
    // Use 'hKey' to encrypt a message.
      }
    // Destroy the session key.
    if(m_hKey != 0) CryptDestroyKey(m_hKey);

    // Release the provider handle.
    if(m_hProv != 0) CryptReleaseContext(m_hProv, 0);
    return NULL;
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 18 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE