HCRYPTPROV hCryptProv; 定义CSP句柄
HCRYPTKEY hKey; 定义密钥句柄
HCRYPTHASH hHash; 定义一个HASH对象的句柄
CHAR szPassword[512] = ""; 定义512大小的字符数组,用来保存密码
DWORD dwLength; 保存密码长度
fprintf(stderr,"Enter a password to be used to create a key:");
GetConsoleInput(szPassword, 512); 获取密码,这个是自己写的函数,目的是在屏幕上显示的是*。
printf("The password has been stored.\n");
dwLength = strlen(szPassword);
if(CryptAcquireContext( 以下是获取一个缺省的PROV_RSA_FULL CSP 句柄
&hCryptProv,
NULL,
NULL,
PROV_RSA_FULL,
0))
{
printf("A context has been acquired. \n");
}
else
{
MyHandleError("Error during CryptAcquireContext!");
}
//--------------------------------------------------------------------
if(CryptCreateHash( 调用CryptCreateHash创建一个HASH对象
hCryptProv, 一个CSP句柄
CALG_MD5, 确定哈希算法
0, 对于非密钥算法,这个参数一定是0,如果是密钥算法,那么这个参数就是密钥
0, 保留参数,为0
&hHash)) 一个哈希对象的指针
{
printf("An empty hash object has been created. \n");
}
else
{
MyHandleError("Error during CryptCreateHash!");
}
//--------------------------------------------------------------------
if(CryptHashData( 调用CryptHashData哈希密码
hHash, 哈希对象
(BYTE *)szPassword, 指向缓冲区的地址
dwLength, 密码长度
0))
{
printf("The password has been hashed. \n");
}
else
{
MyHandleError("Error during CryptHashData!");
}
//--------------------------------------------------------------------
if(CryptDeriveKey( 调用CryptDeriveKey获取对话密码
hCryptProv, CSP句柄
CALG_RC2, 一个ALG_ID结构,用来指定对称密钥生成的算法
hHash, 哈希对象
CRYPT_EXPORTABLE, 指定生成密钥的类型,CRYPT_EXPORTABLE意味着这个程序生成的密钥可以被其它程序调用,而不是仅仅限于这个程序当中。但是它不能用于非对称密码中。
&hKey))
{
printf("The key has been derived. \n");
}
else
{
MyHandleError("Error during CryptDeriveKey!");
}
if(hHash) 销毁哈希对象
{
if(!(CryptDestroyHash(hHash)))
MyHandleError("Error during CryptDestroyHash");
}
if(hKey) 销毁密钥句柄
{
if(!(CryptDestroyKey(hKey)))
MyHandleError("Error during CryptDestroyKey");
}
if(hCryptProv) 销毁CSP句柄
{
if(!(CryptReleaseContext(hCryptProv, 0)))
MyHandleError("Error during CryptReleaseContext");
}
printf("The program to derive a key completed without error. \n");
} // end main
void MyHandleError函数的实现省略,在上一篇中有
void GetConsoleInput(char* strInput,
int intMaxChars)
{
char ch;
char minChar = ' ';
minChar++;
printf("This program creates a session key and duplicates \n");
printf("that key. Next, parameters are added to the original \n");
printf("key. Finally, both keys are destroyed. \n\n");
void main(void)
{
//--------------------------------------------------------------------
// Declare and initialize variables.
HCRYPTPROV hProv; // CSP handle
HCRYPTKEY hSignKey; // Signature key pair handle
HCRYPTKEY hXchgKey; // Exchange key pair handle
HCRYPTKEY hKey; // Session key handle
BYTE *pbKeyBlob; // Pointer to a simple key BLOB
DWORD dwBlobLen; // The length of the key BLOB
//--------------------------------------------------------------------
// Acquire a cryptographic provider context handle.
if(CryptAcquireContext( 获取一个缺省容器的CSP句柄
&hProv,
NULL,
NULL,
PROV_RSA_FULL,
0))
{
printf("The CSP has been acquired. \n");
}
else
{
MyHandleError("Error during CryptAcquireContext.");
}
if(CryptGetUserKey( 获取一个AT_SIGNATURE类型的密钥句柄
hProv,
AT_SIGNATURE,
&hSignKey))
{
printf("The signature key has been acquired. \n");
}
else
{
MyHandleError("Error during CryptGetUserKey for signkey.");
}
//--------------------------------------------------------------------
if(CryptGetUserKey( 获取一个AT_KEYEXCHANGE,类型的密钥句柄,保存在hXchgKey中
hProv,
AT_KEYEXCHANGE,
&hXchgKey))
{
printf("The key exchange key has been acquired. \n");
}
else
{
printf("Error during CryptGetUserKey exchange key.");
}
// Generate a session key.
if (CryptGenKey( 生成一个CRYPT_EXPORTABLE(可导出的),CALG_RC4(指定算法)的密钥,保存在hKey
hProv,
CALG_RC4,
CRYPT_EXPORTABLE,
&hKey))
{
printf("Original session key is created. \n");
}
else
{
MyHandleError("ERROR -- CryptGenKey.");
}
if(CryptExportKey( CryptExportKey导出一个密钥
hKey, 将要导出的密钥的句柄
hXchgKey, 用户最终使用到的密钥的句柄
SIMPLEBLOB, 指定BLOB的类型,SIMPLEBLOB说明是 用来导出对话密钥的
0, 指定密钥的附加属性
NULL,
&dwBlobLen)) 当时这个函数在这里的主要
目的是得到这个BLOB的长度
{
printf("Size of the BLOB for the session key determined. \n");
}
else
{
MyHandleError("Error computing BLOB length.");
}
if(pbKeyBlob = (BYTE*)malloc(dwBlobLen))
{
printf("Memory has been allocated for the BLOB. \n");
}
else
{
MyHandleError("Out of memory. \n");
}
if(CryptExportKey( 这是这个函数才是真正的导出密钥
hKey,
hXchgKey,
SIMPLEBLOB,
0,
pbKeyBlob,
&dwBlobLen))
{
printf("Contents have been written to the BLOB. \n");
}
else
{
MyHandleError("Error during CryptExportKey.");
}
free(pbKeyBlob); 释放内存
// Destroy the session key.
if(hKey)
CryptDestroyKey(hKey);
// Destroy the signature key handle.
if(hSignKey)
CryptDestroyKey(hSignKey);
// Destroy the key exchange key handle.
if(hXchgKey)
CryptDestroyKey(hXchgKey);
// Release the provider handle.
if(hProv)
CryptReleaseContext(hProv, 0);
printf("The program ran to completion without error. \n");