Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CryptoAPI inside windows service

12 views
Skip to first unread message

Andrei

unread,
Mar 16, 2004, 2:33:49 AM3/16/04
to
I have trouble when I use CryptoAPI inside windows service.
When I try to import 3DES key by CryptImportKey function
it returns error code NTE_BAD_KEY. In fact all work ok
when I build project as console application.
I try to use CRYPT_MACHINE_KEYSET flag in
CryptAcquireContext. But it still doesn't work.
Please see my source and give me idea! :-)

if(!CryptAcquireContext
(&hProv,"_hprvkey1",MS_ENHANCED_PROV,PROV_RSA_FULL,0 /*CRYP
T_MACHINE_KEYSET*/) && !CryptAcquireContext
(&hProv,"_hprvkey1",MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_NE
WKEYSET /*| CRYPT_MACHINE_KEYSET*/))
{
ResCode = -1;
}
// Open key from file
RSAPubKey1024 PubKey;
dwLen=sizeof(RSAPubKey1024);

CFile dskFile;
if(!dskFile.Open(KeyFileName.c_str
(),CFile::modeRead))
{
ResCode = -10034;
goto Ennd;
}
// Read
if(dskFile.Read(&PubKey,dwLen) != dwLen)
{
ResCode = -101;
goto Ennd;
}
dskFile.Close();
// Import key
if(!CryptImportKey(hProv,(unsigned char *)
&PubKey,dwLen,0,0 ,&wrkKey))
{
DWORD Err = GetLastError();
ResCode = -102;
goto Ennd;
}
// Create HASH
if(!CryptCreateHash(hProv,CALG_MD5,0,0,&hHash))
{
ResCode = -103;
goto Ennd;
}
// Read 3DES key from file
if (Data->Read(&EncryptedPartHeader, sizeof
(EncryptedPartHeader)) != sizeof(EncryptedPartHeader))
{
ResCode = -104;
goto Ennd;
}
// Import 3DES key
//********* here function return NTE_BAD_KEY error code
if(!CryptImportKey(hProv,(BYTE *)
&EncryptedPartHeader.kb,EncryptedPartHeader.KeyLen/*sizeof
(RSA1024KeyExchBLOB)*/,wrkKey,0,&h3DESKey))
{
DWORD Err = GetLastError();
ResCode = -105;
goto Ennd;
}

Ryan Menezes [MSFT]

unread,
Mar 16, 2004, 3:27:38 PM3/16/04
to
Typically this scenario should work if you are using MACHINE_KEYSET.

In the first CryptImportKey call did you import a PRIVATEKEYBLOB ? I am
guessing you imported a PUBLICKEYBLOB and hence it does not have the private
key to decrypt the SIMPLEBLOB in the next step so it thinks the key is bad.

--
Thanks,
Ryan Menezes [MS]
This posting is provided "AS IS" with no warranties, and confers no rights.


"Andrei" <scher...@sysoft.ru> wrote in message
news:db7201c40b29$05c90000$a301...@phx.gbl...

0 new messages