CryptImportKey Fails.-Strange problem

B

Badrinath Mohan

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();
}
 
R

Ryan Menezes [MSFT]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top