⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inituser.c

📁 C++编程实践与技巧一书各章节的源码
💻 C
字号:

#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);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -