error with CryptImportKey and large or small amounts of data
From: Andy Lowe (andyl_at_dmetrix.com)
Date: 03/11/04
- Next message: Mark_Pryor: "Re: error with CryptImportKey and large or small amounts of data"
- Previous message: Chandra Tiwary: "MS Authorization Manger"
- Next in thread: Mark_Pryor: "Re: error with CryptImportKey and large or small amounts of data"
- Reply: Mark_Pryor: "Re: error with CryptImportKey and large or small amounts of data"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: 10 Mar 2004 16:57:24 -0800
Hello-
I'm working on writing some code to do PBKDF2 encryption, using the
examples in Viega and Messier's "Secure Programming Cookbook" as a
guide. The code works fine with strings of 5-16 characters. With
less than 5 or more than 16 characters, a call to CryptImportKey
fails. GetLastError() returns NTE_BAD_FLAGS, although the flags are
the same for all string lengths. I'm using the enhanced provider with
PROV_RSA_FULL. The line that fails is at the bottom of the following
snippet.
if (!::CryptGetUserKey(hProvider, AT_KEYEXCHANGE, &hImportKey))
{
if (::GetLastError() == NTE_NO_KEY)
{
//the key doesn't exist, so create it
if (!::CryptGenKey(hProvider, AT_KEYEXCHANGE, (1024 << 16),
&hImportKey))
{
hImportKey = NULL;
}
}
else
{
hImportKey = NULL;
}
}
//if the user's key was acquired successfully, figure out how much
space is needed and allocate it
if (hImportKey != NULL &&
::CryptEncrypt(hImportKey, 0, TRUE, 0, 0, &dwDataSize, dwDataSize) &&
(pbData = static_cast<BYTE *>(::LocalAlloc(LMEM_FIXED, dwDataSize +
dwHeaderLen))) != NULL)
{
//copy the key's data into the allocated block
::CopyMemory(pbData + dwHeaderLen, pbKeyData, dwKeyDataSize);
dwKeyLen = dwKeyDataSize;
//encrypt the data
if (::CryptEncrypt(hImportKey, 0, TRUE, 0, pbData + dwHeaderLen,
&dwKeyLen, dwDataSize))
{
pBlob = reinterpret_cast<BLOBHEADER *>(pbData);
pAlgID = reinterpret_cast<ALG_ID *>(pbData + sizeof(BLOBHEADER));
pBlob->bType = SIMPLEBLOB;
pBlob->bVersion = 2;
pBlob->reserved = 0;
pBlob->aiKeyAlg = AlgID;
dwDataLen = sizeof(ALG_ID);
//transfer the key over to the provider
if (!(bResult = ::CryptGetKeyParam(hImportKey, KP_ALGID,
reinterpret_cast<BYTE *>(pAlgID),
&dwDataLen, 0)))
{
DWORD dwError = ::GetLastError();
std::cerr << "CryptGetKeyParam failed. Error: " << dwError;
}
//-----------this fails------------------------
else if (!(bResult = ::CryptImportKey(hProvider,
pbData,
dwDataSize + dwHeaderLen,
hImportKey,
0,
&hKey)))
{
DWORD dwError = ::GetLastError();
std::cerr << "CryptImportKey failed. Error: " << dwError;
}
}
}
Any ideas? Thanks.
Andy
- Next message: Mark_Pryor: "Re: error with CryptImportKey and large or small amounts of data"
- Previous message: Chandra Tiwary: "MS Authorization Manger"
- Next in thread: Mark_Pryor: "Re: error with CryptImportKey and large or small amounts of data"
- Reply: Mark_Pryor: "Re: error with CryptImportKey and large or small amounts of data"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|