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

📄 initkey.c

📁 C++编程实践与技巧一书各章节的源码
💻 C
字号:
// genkey.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wincrypt.h>

#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void HandleError(char *s);

void  main(void) 
{ 
// Declare and initialize variables.

HCRYPTPROV hCryptProv;        // Handle for the 
                              // cryptographic provider context.
HCRYPTKEY hKey;               // Public/private key handle.
CHAR szUserName[100];         // Buffer to hold the name 
                              // of the key container.
DWORD dwUserNameLen = 100;    // Length of the buffer.
LPCSTR UserName= NULL;        // Optionally enter the user's name here
                              // to be used as the key container
                              // name (limited to 100 characters).
//--------------------------------------------------------------------
// Begin processing. Attempt to acquire a context with a default key
// container.

//  To create a new key container, substitute a string for the
//  NULL second parameter here and in the next call to 
//  CryptAcquireContext. 

if(CryptAcquireContext(
   &hCryptProv,               // Handle to the CSP
   UserName,                  // Container name 
   MS_DEF_PROV,               // Provider name
   PROV_RSA_FULL,             // Provider type
   0))                        // Flag values
{
    printf("A crypto context with thesdgddsdfgsdfgsdfg %s key container \n", UserName);
    printf("has been acquired.\n\n");
}
else
{ 
//--------------------------------------------------------------------
// Some sort of error occurred in acquiring the context. 
// Create a new default key container. 

   if(CryptAcquireContext(
      &hCryptProv, 
      UserName, 
      MS_DEF_PROV, 
      PROV_RSA_FULL, 
      CRYPT_NEWKEYSET)) 
   {
      printf("A new key container has been created.\n");
   }
   else
   {
      HandleError("Could not create a new key container.\n");
    }
} // End of else
//--------------------------------------------------------------------
// A cryptographic context and a key container is available. Get the
// name of the key container. 
if(CryptGetProvParam(
    hCryptProv,               // Handle to the CSP
    PP_CONTAINER,             // Get the key container name 
    (BYTE *)szUserName,       // Pointer to the key container name
    &dwUserNameLen,           // Length of name, preset to 100
    0)) 
{
    printf("A crypto context has been acquired and \n");
    printf("The name on the key container is %s\n\n",szUserName);
}
else
{
    // Error getting key container name
    HandleError("A context was acquired or created, but\
      an error occurred getting the key container name.\n");
} 

//--------------------------------------------------------------------
// A context with a key container is available,
// Attempt to get the handle to key exchange key. 

if(CryptGetUserKey(
   hCryptProv,                     // Handle to the CSP
   AT_SIGNATURE,                   // Key specification
   &hKey))                         // Handle to the key
{
    printf("A signature key is available.\n");
}
else
{
    printf("No signature key is available.\n");
    if(GetLastError() == NTE_NO_KEY) 
    {
    //----------------------------------------------------------------
    // The error was that there is a container but no key.

    // Create a signature key pair. 
       printf("The signature key does not exist.\n");
       printf("Create a signature key pair.\n"); 
       if(CryptGenKey(
          hCryptProv,
          AT_SIGNATURE,
          0,
          &hKey)) 
       {
          printf("Created a signature key pair.\n");
       }
       else
       {
          HandleError("Error occurred creating a signature key.\n"); 
       }
    }
    else
    {
        HandleError("An error other than NTE_NO_KEY getting signature\
            key.\n");
    }
} // End of if

printf("A signature key pair existed, or one was created.\n\n");
CryptDestroyKey(hKey); 

// Next, check the exchange key. 
if(CryptGetUserKey(
   hCryptProv,
   AT_KEYEXCHANGE,
   &hKey)) 
{
   printf("An exchange key exists. \n");
}
else
{
     printf("No exchange key is available.\n");
     // Check to see if one needs to be created.
     if(GetLastError()==NTE_NO_KEY) 
     { 
       // Create an key exchange key pair.
       printf("The exchange key does not exist.\n");
       printf("Attempting to create an exchange key pair.\n");
       if(CryptGenKey(
           hCryptProv,
           AT_KEYEXCHANGE,
           0,
           &hKey)) 
       {
           printf("Exchange key pair created.\n");
       }
       else
       {
          HandleError("Error occurred attempting to create \
             an exchange key.\n");
       }
    }
    else
    {
       HandleError("An error other than NTE_NO_KEY occurred.\n");
     }
}

printf("An exchange key pair existed, or one was created.\n\n");
CryptDestroyKey(hKey); 
CryptReleaseContext(hCryptProv,0); 
printf("Everything 实打实大苏is okay. A signature key\n");
printf("pair and an exchange key exist in\n");
printf("the %s key container.\n",UserName);  
} // 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 + -