给你一段代码。具体请查看msdn #include <stdio.h> #include <windows.h> #include <wincrypt.h> #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING) void HandleError(char *s);void main() {//-------------------------------------------------------------------- // Declare variables.HCRYPTPROV hCryptProv; HCRYPTHASH hHash; HCRYPTKEY hKey;//-------------------------------------------------------------------- // Begin processing.printf("Process beginning. Creating a session key. \n"); //-------------------------------------------------------------------- // Get a handle to the default provider.if(CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) { printf("CryptAcquireContext complete. \n"); } else { HandleError("Acquisition of context failed."); } //-------------------------------------------------------------------- // Create a hash object.if(CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash)) { printf("An empty hash object has been created. \n"); } else { HandleError("Error during CryptBeginHash!\n"); } //-------------------------------------------------------------------- // Create a random session key.if(CryptGenKey( hCryptProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey)) { printf("A random session key has been created. \n"); } else { HandleError("Error during CryptGenKey!\n"); } //-------------------------------------------------------------------- // Compute the cryptographic hash on the key object.if(CryptHashSessionKey( hHash, hKey, 0)) { printf("The session key has been hashed. \n"); } else { HandleError("Error during CryptHashSessionKey!\n"); } // Use the hash of the key object. For instance, additional // data could be hashed and sent in a message to several recipients. // The recipients will be able to verify who the message originator // is if the key used is also exported to them.//-------------------------------------------------------------------- // Clean up.// Destroy the hash object.if(hHash) CryptDestroyHash(hHash);// Destroy the session key.if(hKey) CryptDestroyKey(hKey);// Release the CSP.if(hCryptProv) CryptReleaseContext(hCryptProv,0);printf("Create random session key completed without error. \n"); } // End of main//-------------------------------------------------------------------- // This example uses the function HandleError, a simple error // handling function, to print an error message and exit // the program. // For most applications, replace this function with one // that does more extensive error reporting.void HandleError(char *s) { printf("An error occurred in running the program.\n"); printf("%s\n",s); printf("Error number %x\n.",GetLastError()); printf("Program terminating.\n"); exit(1); }
很不错的呀
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void HandleError(char *s);void main()
{//--------------------------------------------------------------------
// Declare variables.HCRYPTPROV hCryptProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;//--------------------------------------------------------------------
// Begin processing.printf("Process beginning. Creating a session key. \n");
//--------------------------------------------------------------------
// Get a handle to the default provider.if(CryptAcquireContext(
&hCryptProv,
NULL,
NULL,
PROV_RSA_FULL,
0))
{
printf("CryptAcquireContext complete. \n");
}
else
{
HandleError("Acquisition of context failed.");
}
//--------------------------------------------------------------------
// Create a hash object.if(CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
printf("An empty hash object has been created. \n");
}
else
{
HandleError("Error during CryptBeginHash!\n");
}
//--------------------------------------------------------------------
// Create a random session key.if(CryptGenKey(
hCryptProv,
CALG_RC2,
CRYPT_EXPORTABLE,
&hKey))
{
printf("A random session key has been created. \n");
}
else
{
HandleError("Error during CryptGenKey!\n");
}
//--------------------------------------------------------------------
// Compute the cryptographic hash on the key object.if(CryptHashSessionKey(
hHash,
hKey,
0))
{
printf("The session key has been hashed. \n");
}
else
{
HandleError("Error during CryptHashSessionKey!\n");
}
// Use the hash of the key object. For instance, additional
// data could be hashed and sent in a message to several recipients.
// The recipients will be able to verify who the message originator
// is if the key used is also exported to them.//--------------------------------------------------------------------
// Clean up.// Destroy the hash object.if(hHash)
CryptDestroyHash(hHash);// Destroy the session key.if(hKey)
CryptDestroyKey(hKey);// Release the CSP.if(hCryptProv)
CryptReleaseContext(hCryptProv,0);printf("Create random session key completed without error. \n");
} // End of main//--------------------------------------------------------------------
// This example uses the function HandleError, a simple error
// handling function, to print an error message and exit
// the program.
// For most applications, replace this function with one
// that does more extensive error reporting.void HandleError(char *s)
{
printf("An error occurred in running the program.\n");
printf("%s\n",s);
printf("Error number %x\n.",GetLastError());
printf("Program terminating.\n");
exit(1);
}