Click to See Complete Forum and Search --> : problem with CryptGetKeyParam()
hahaha
July 25th, 2005, 11:53 PM
--------------------------------------------------------------------------------
I'm using CryptGen key to generate keys of specific length but when i call CryptGetKeyParam to print the length of the key it doesnt show.
actually i give user choices of keylength in a combo box whos variable is passed in the CryptGenKey function.
The code is like
int m_keysize; //member variable of combo box
if (!CryptGenKey(hCryptProv, AT_SIGNATURE,DWORD(m_keysize), &hKey))
{
// Error during CryptGenKey!
}
if(CryptGetKeyParam(
hKey,
KP_KEYLEN,
pbData,
&dwCount,
0))
{
MessageBox((LPCTSTR)pbData,"Key size",MB_OK);
}
All the variables are declared and keys are successfully generated as well.but the key length is not shown in the message box i.e empty message box
olivthill
July 26th, 2005, 03:43 AM
The syntax of CryptGetKeyParam() can be seen at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/seccrypto/security/cryptgetkeyparam.asp
...
KP_KEYLEN Retrieve the actual length of the key. The pbData parameter is a pointer to a DWORD value that receives the key length, in bits. KP_KEYLEN can be used to get the length of any key type. Microsoft cryptographic service providers (CSPs) return a key length of 64 bits for CALG_DES, 128 bits for CALG_3DES_112, and 192 bits for CALG_3DES. These lengths are different from the lengths returned when you are enumerating algorithms with the dwParam value of the CryptGetProvParam function set to PP_ENUMALGS. The length returned by this call is the actual size of the key, including the parity bits included in the key.
Microsoft CSPs that support the CALG_CYLINK_MEK ALG_ID return 64 bits for that algorithm. CALG_CYLINK_MEK is a 40-bit key but has parity and zeroed key bits to make the key length 64 bits.
...
So, since pbData is a pointer to a DWORD, not a pointer to a string of characters, you have to convert the value before displaying it in your MessageBox.
hahaha
July 27th, 2005, 11:54 PM
if(CryptGetKeyParam(
hKey,
KP_KEYLEN,
NULL,
&dwCount,
0))
{
// Allocate memory for the pbData.
if(pbLen = (BYTE*)malloc(dwCount))
{
MessageBox("Memory has been allocated for the blob.");
}
else
{
MessageBox("Out of memory.");
}
}
if(CryptGetKeyParam(
hKey,
KP_KEYLEN,
pbLen,
&dwCount,
0))
{
MessageBox((LPCTSTR)pbLen,"Key size",MB_OK);
}
here i do like this but it still doesnt show the length of the key
olivthill
July 28th, 2005, 04:15 AM
The problem is still in the line
MessageBox((LPCTSTR)pbLen,"Key size",MB_OK);
pbLen is not a pointer to an array containing nice and sociable visible digits, but it contains your value as a compact and shy binary number. You have to convert it, so that you will see it. For instance, you can do:
char msgbuf[100];
sprintf(msgbuf, "%d", pbLen);
MessageBox(msgbuf,"Key size",MB_OK);
hahaha
July 31st, 2005, 11:51 PM
char msgbuf[100];
sprintf(msgbuf, "%d", pbLen);
MessageBox(msgbuf,"Key size",MB_OK);
Ive used this code..but in the msg box I get values like "7805163"...what this mean....even I've set the key length for RSA as 512 bits.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.