none
ImportCspBlob - does it work?

    Question

  • Has anyone had any luck with the ImportCspBlob method of RSACryptoServiceProvider? I am trying to verify a signature that was created with an unmanaged app. So, I export the public key blob to a file from the unmanaged app like so:

    CryptExportKey(m_hKey,0,PUBLICKEYBLOB,0,NULL,&dwBlobSize)
    BYTE *pbData =
    new BYTE[dwBlobSize];
    CryptExportKey(m_hKey,0,PUBLICKEYBLOB,0,pbData,&dwBlobSize);

    HANDLE hFile = CreateFile("public.key",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,0,NULL);

    if (hFile != INVALID_HANDLE_VALUE)
    {
      DWORD dwNumberOfBytesWritten = 0;

        if (WriteFile(hFile,pbData,dwBlobSize,&dwNumberOfBytesWritten,NULL))
       
    {
         
    bRet = TRUE;
       
    }

        CloseHandle(hFile);
    }

    Then, in my managed app I read the file and attempt the ImportCspBlob call:

    CspParameters cspParms = new CspParameters();
    cspParms.KeyContainerName =
    "Blah";
    RSACryptoServiceProvider _csp =
    new RSACryptoServiceProvider(cspParms);

    FileStream fs = new FileStream("public.key", FileMode.Open, FileAccess.Read);
    Byte[] fileBytes = new Byte[fs.Length];
    fs.Read(fileBytes, 0, (
    int) fs.Length);
    fs.Close();

    _csp.ImportCspBlob(fileBytes);

    A couple of odd things happen. First, when I create the CryptoProvider object it generates a new key pair (this is fine). However, after importing the CSP blob the key appears to be unchanged. There were no excpetions generated in the import step. Of course, using the wrong key my signature verifications fails.

    Any wisdom?

    Friday, August 04, 2006 3:55 PM