CryptGetUserKey works in WinXP but fails in Win2000

W

WilOJoe

Does anyone have any ideas about why the Crypto API call to
CryptGetUserKey(…)would work just fine in WindowsXP but fails when
executed in Windows2000? The call is made in code that is practically
identical to the well known file encryption example found in the MSDN
help (see ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/security/security/example_c_program_encrypting_a_file.htm
if you have MSDN installed on your machine) and attempts to use the
AT_KEYEXCHANGE private key from the key container. GetLastError() is
returning NTE_NO_KEY signifying that "The key requested by the
dwKeySpec parameter does not exist."

Any assistance is appreciated,

OrrisJ


------------- CODE FRAGMENT BELOW ------------------------------------
IDataBufferPtrHandle Encrypt( IDataBufferPtrHandle pdbUnEncryptedData
)
{

***** Code Deleted ***************

///////////////////////////////////////////////////////
// Get the handle to the default key container within
// the default cryptographic service provider (CSP).
///////////////////////////////////////////////////////

if( !CryptAcquireContext( &m_hCryptProv, NULL, MS_ENHANCED_PROV,
PROV_RSA_FULL, 0 ) )
{
CErrorMsg( method, _T( "Error %x during CryptAcquireContext!\n" ),
GetLastError() );
CleanUp();
return NULL;
}

///////////////////////////////////////////////////////
// Encrypt using a random session key derived from
// the current users windows credentials. The ramdom
// session key and its size is saved along with the
// data.
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
// Create a random session key.

if( !CryptGenKey( m_hCryptProv, ENCRYPT_ALGORITHM, KEYLENGTH |
CRYPT_EXPORTABLE, &m_hKey ) )
{
CErrorMsg( method, _T( "Error %x during CryptGenKey.\n" ),
GetLastError() );
CleanUp();
return NULL;
}

///////////////////////////////////////////////////////
// Get the handle to the encrypter's exchange public
// key.

if( !CryptGetUserKey( m_hCryptProv, AT_KEYEXCHANGE, &m_hXchgKey ) )
{
CErrorMsg( method, _T( "Error %x User public key is not available
and may not exist.\n" ), GetLastError() );
CleanUp();
return NULL;
}

***** Code Deleted ***************


///////////////////////////////////////////////////////
// Export the session key into a simple key BLOB.

if( !CryptExportKey( m_hKey, m_hXchgKey, SIMPLEBLOB, 0, m_pbKeyBlob,
&dwKeyBlobLength ) )
{
CErrorMsg( method, _T( "Error %x during CryptExportKey!\n" ),
GetLastError() );
CleanUp();
return NULL;
}

***** Code Deleted ***************

///////////////////////////////////////////////////////
// Encrypt the data. After the successfull call to
// CryptEncrypt, dwDataLength will contain the length
// of the encrypted buffer.
///////////////////////////////////////////////////////

if( !CryptEncrypt( m_hKey, 0, TRUE, 0, m_pbBuffer, &dwDataLength,
dwBufferLength))
{
CErrorMsg( method, _T( "Error %x during CryptEncrypt!\n" ),
GetLastError() );
CleanUp();
return NULL;
}

///////////////////////////////////////////////////////
// Save key, key length, encrypted data and data length
// into the data buffer.

***** Code Deleted ***************

CleanUp();
return pIEncryptedData;
}
 
V

Victor Bazarov

WilOJoe said:
Does anyone have any ideas about why the Crypto API call to
CryptGetUserKey(.)would work just fine in WindowsXP but fails when
executed in Windows2000? [..][

Somebody in a newsgroup where it's on topic is likely able
to answer this. Try comp.os.ms-windows.programmer.win32

Victor
 
W

WilOJoe

Thanks...I will try posting to comp.os.ms-windows.programmer.win32 as you suggest.

OrrisJ
 

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

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top