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.2003 FEB.1033/security/security/example_c_progr am_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 ------------------------------------
IDataBufferPtrH andle Encrypt( IDataBufferPtrH andle pdbUnEncryptedD ata
)
{
***** Code Deleted ***************
///////////////////////////////////////////////////////
// Get the handle to the default key container within
// the default cryptographic service provider (CSP).
///////////////////////////////////////////////////////
if( !CryptAcquireCo ntext( &m_hCryptPro v, NULL, MS_ENHANCED_PRO V,
PROV_RSA_FULL, 0 ) )
{
CErrorMsg( method, _T( "Error %x during CryptAcquireCon text!\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_ALGORIT HM, KEYLENGTH |
CRYPT_EXPORTABL E, &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( !CryptGetUserKe y( 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,
&dwKeyBlobLengt h ) )
{
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, &dwDataLengt h,
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 ;
}