Re: CryptImportKey Fails.-Strange problem

From: Ryan Menezes [MSFT] (ryanmen_at_online.microsoft.com)
Date: 11/18/03


Date: Tue, 18 Nov 2003 12:36:19 -0800

you need to first CryptGetParam the P and G from the first container and
then set it into the second container using CryptSetKeyParam before
importing the PUBLICKEYBLOB for a DiffieHellman exchange. After that use
CryptSetKEyParam (KP_ALGID) to convert the agreed key to a symmetric key

Thanks,
Ryan

"Badrinath Mohan" <bmohan@NOSPAMuncc.edu> wrote in message
news:OQE4M9rqDHA.644@TK2MSFTNGP11.phx.gbl...
> Hi guys
> This is strange but Cryptimportkey returns with NTE_BAD_KEY
> I create keys using Diffie Hellman CSP and then export it.
> I import it in another function but it fails saying Bad Key.
> So i decided to immediately import it after exporting to check it out..
> Here also it fails with NTE_BAD_KEY
> Any comments?
> Thanks and waiting for ur replies
> Badri
>
> ----------------------Attached --------------- -----
> HCRYPTPROV hProv;
> HCRYPTKEY hKey;
> HCRYPTKEY hSecretKey;
> BYTE *pbyKeyData;
> DWORD dwKeyLen=0;
> HANDLE hPublicFile;
> DWORD dwBytesWritten=0;
> int nError=0;
>
> if(!CryptAcquireContext(&hProv, "TestImport",
> MS_ENH_DSS_DH_PROV,PROV_DSS_DH, 0))
> {
> TRACE("\nError during CryptAcquireContext!");
> }
> if(CryptGenKey(hProv,CALG_DH_EPHEM,CRYPT_EXPORTABLE,&hKey))
> {
> TRACE("\nA session key has been created");
> }
> else
> {
> TRACE("\nError during CryptGenKey");
> int nk=GetLastError();
> exit(1);
> }
>
> BYTE *pbData=0;
> DWORD dwDataLen=0;
>
> if(!CryptExportKey(hKey,0,PUBLICKEYBLOB,0,NULL,&dwKeyLen))
> TRACE("\nError during CryptExportKey");
> pbyKeyData=new BYTE[dwKeyLen];
> if(!CryptExportKey(hKey,0,PUBLICKEYBLOB,0,pbyKeyData,&dwKeyLen))
> TRACE("\nError during CryptExportKey");
>
> //i export it into a file ...and then do a import in another function.
> It fails there so i have tested cryptimportkey in the same
> function after the export and it fails here tooo...
>
>
> if(!CryptImportKey(hProv,pbyKeyData,dwKeyLen,0,0,&hSecretKey))
> {
> TRACE("\nError during CryptImportKey!");
> nError=GetLastError();
> }
>
>