locked
The function CryptImportKey() throws a error "Bad Version of provider" RRS feed

  • Question

  •   My code call the function CryptImportKey(), then throws a error "Bad Version of provider".

      At first I generate a keypair in the CSP's Keypair Container, then export the publickey into a keyblob,
      But when I import the same keyblob into the CSP, it throws a exception "Bad Version of provider", the error code is NTE_BAD_VER(0x80090007L). What is the problem with my code?

      Below is the code, thanks for your attention.

     

    if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
    {
     if(!CryptAcquireContext(&hProv,szContainer,MS_ENHANCED_PROV,
       PROV_RSA_FULL,CRYPT_NEWKEYSET))
     {
      printf("CryptAcquireContext error!\n");
      exit(1);
     }
    }

    if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
    {
     if(GetLastError() == NTE_NO_KEY)
     {
      if(!CryptGenKey(hProv,AT_SIGNATURE,0,&hKey))
      {
       printf("CryptGenKey error!\n");
       exit(1);
      }
     }
    }


    DWORD dwBlobLen=0;
    BYTE* pbKeyBlob=NULL;
    if(!CryptExportKey( 
     hKey, 
     NULL, 
     PUBLICKEYBLOB,
     0, 
     NULL,
     &dwBlobLen))
    {
     printf("CryptExportKey error!\n");
     exit(1);
    }

    //--------------------------------------------------------------------
    // Allocate memory for the pbKeyBlob.
    if(pbKeyBlob = (BYTE*)malloc(dwBlobLen))
    {
     TRACE("Memory has been allocated for the BLOB. \n");
    } else {
     printf("Out of memory!\n");
     exit(1);
    }

    //--------------------------------------------------------------------
    // Do the actual exporting into the key BLOB.
    if(!CryptExportKey( 
     hKey,
     NULL, 
     PUBLICKEYBLOB,
     0, 
     pbKeyBlob,
     &dwBlobLen))
    {
     printf("Error exporting key!\n");
     delete [] pbKeyBlob;
     exit(1);
    }

    //...


    //--------------------------------------------------------------------
    // here throws a "Bad Version of provider" error.
    if (!CryptImportKey(hProv, (BYTE*)&pbKeyBlob, dwBlobLen, NULL, 0, &hKey))
    {
     // Bad Version of provider
     printf("CryptImportKey Error!\n");
     exit(1);
    }

    delete [] pbKeyBlob;
    if(hKey) CryptDestroyKey(hKey);
    CryptReleaseContext(hProv,0);

    • Edited by sun_key Friday, July 30, 2010 1:44 AM adjust thetext
    Thursday, July 29, 2010 8:28 AM

Answers

  • Hi,

    You have a bug in the code that calls CryptImportKey, specifically the second parameter : replace  (BYTE*)&pbKeyBlob by pbKeyBlob and everything will be OK!

    Cheers,
    --
    Mounir IDRASSI
    IDRIX
    http://www.idrix.fr

    • Marked as answer by sun_key Monday, August 2, 2010 3:56 AM
    Sunday, August 1, 2010 12:33 AM

All replies

  • Hi,

    You have a bug in the code that calls CryptImportKey, specifically the second parameter : replace  (BYTE*)&pbKeyBlob by pbKeyBlob and everything will be OK!

    Cheers,
    --
    Mounir IDRASSI
    IDRIX
    http://www.idrix.fr

    • Marked as answer by sun_key Monday, August 2, 2010 3:56 AM
    Sunday, August 1, 2010 12:33 AM
  • OK, it works now. Thank you.

    Monday, August 2, 2010 3:56 AM