Re: CryptoAPI Hard Coding Keys, Help
- From: Colin <Colin@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 27 Dec 2006 06:58:01 -0800
I get an error NTE_BAD_KEY with the following code:
void TestCase3()
{
// Declare and initialize variables.
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTKEY hXchgKey;
BYTE pbData[1000]; // 1000 will hold the longest
// key container name.
DWORD cbData;
char msg1[] = "fuzzy wuzzy was a bear";
BYTE pbData2[2048];
DWORD bufLen, bufLen2;
BOOL fSuccess;
// if( CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, 0) ==
false )
if( CryptAcquireContext( &hCryptProv, TEXT("RtsKeys"), NULL, PROV_RSA_FULL,
0) == false )
{
if (GetLastError() == 0x80090016 )
{
printf(" The default key container does not exist.\n");
printf(" Create a default container and generate keys \n");
printf(" Using the code in \n");
printf(" Example C Program: Creating a Key Container and "
"Generating Keys \n");
printf(" Before running this program. \n");
if( CryptAcquireContext( &hCryptProv, TEXT("RtsKeys"), NULL,
PROV_RSA_FULL, CRYPT_NEWKEYSET ) == false )
{
printf( "Failed to create new key container\n" );
exit(0);
}
// exit(0);
}
else
{
MyHandleError("A general error running "
"CryptAcquireContext.");
}
}
//if ( CryptGetUserKey( hCryptProv, AT_KEYEXCHANGE, &hXchgKey) == false )
//{
// ReportWinError( TEXT("Error during CryptGetUserKey exchange key.")
);
// exit( 1 );
//}
if( CryptGenKey( hCryptProv, CALG_RC4, 0x00800000 | CRYPT_EXPORTABLE,
&hKey) == false )
// if( CryptGenKey( hCryptProv, CALG_RC4, 0x00800000 | CRYPT_ARCHIVABLE,
&hKey) == false )
{
ReportWinError( TEXT("Error during CryptGenKey. \n") );
exit( 1 );
}
bufLen = sizeof( pbData );
// if ( CryptExportKey( hKey, hXchgKey, PRIVATEKEYBLOB, 0, &pbData[0],
&bufLen ) == false )
if ( CryptExportKey( hKey, NULL, PRIVATEKEYBLOB, 0, &pbData[0], &bufLen
) == false )
{
ReportWinError( TEXT("Error exporting key.\n") );
exit( 1 );
}
bufLen2 = strlen( ( char* )pbData );
if ( bufLen2 == bufLen )
printf( "Exported key is in text form\n" );
else
printf( "Exported key is binary: buflen is %d and strlen is %d\n",
bufLen, bufLen2 );
CryptReleaseContext( hCryptProv, 0 );
}
"Mark Pryor" wrote:
On Fri, 22 Dec 2006 13:30:00 -0800, Colin wrote:.
I am trying to create a license scheme for my application. Part of my
implementation requires that I generate keys then export them, and then hard
code those values in my applicaiton.
Now please, I don't need a discussion on wether I should do that or not.
I am trying to generate keys and then export them using CryptExportKey. I
am using the RSA provider. Does anyone have an example program I can use
that will generate RSA keys, then export the private and public keys so that
I can then re-import them on another computer and test that an application on
two computers can encrypt and decrypt a license string or some other
encrypted data I pass between them.
I have tried several different test apps, I have experienced several
different problems, but basically, I can not get CryptExportKey to work with
RSA keys.
Colin,
You can export the whole container at once -- that's both sides of the key
pair -- using CryptExportKey() with dwBlobType=PRIVATEKEYBLOB.
If your container has valid RSA keys in the first place, CryptExportKey
should easily work. Instead of asking for new sample code, why not try and
troubleshoot your current code. Post any errors and relevant snips.
--
Mark
- Follow-Ups:
- Re: CryptoAPI Hard Coding Keys, Help
- From: lelteto
- Re: CryptoAPI Hard Coding Keys, Help
- References:
- Re: CryptoAPI Hard Coding Keys, Help
- From: Mark Pryor
- Re: CryptoAPI Hard Coding Keys, Help
- Prev by Date: Re: No logon sid in the w2k sp4
- Next by Date: Re: Using CryptoAPI to do a DH key exchange with OpenSSL
- Previous by thread: Re: CryptoAPI Hard Coding Keys, Help
- Next by thread: Re: CryptoAPI Hard Coding Keys, Help
- Index(es):
Relevant Pages
|