home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / crypto / inituser / inituser.c next >
C/C++ Source or Header  |  1997-10-08  |  3KB  |  85 lines

  1. /******************************************************************************\
  2. *       This is a part of the Microsoft Source Code Samples. 
  3. *       Copyright 1996-1997 Microsoft Corporation.
  4. *       All rights reserved. 
  5. *       This source code is only intended as a supplement to 
  6. *       Microsoft Development Tools and/or WinHelp documentation.
  7. *       See these sources for detailed information regarding the 
  8. *       Microsoft samples programs.
  9. \******************************************************************************/
  10.  
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <wincrypt.h>
  15.  
  16. /*****************************************************************************/
  17. void _cdecl main(void)
  18. {
  19.     HCRYPTPROV hProv;
  20.     HCRYPTKEY hKey;
  21.     CHAR szUserName[100];
  22.     DWORD dwUserNameLen = 100;
  23.  
  24.     // Attempt to acquire a handle to the default key container.
  25.     if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) {
  26.     // Some sort of error occured.
  27.  
  28.     // Create default key container.
  29.     if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
  30.         printf("Error creating key container!\n");
  31.         exit(1);
  32.     }
  33.  
  34.     // Get name of default key container.
  35.     if(!CryptGetProvParam(hProv, PP_CONTAINER, szUserName, &dwUserNameLen, 0)) {
  36.         // Error getting key container name.
  37.         szUserName[0] = 0;
  38.     }
  39.  
  40.     printf("Create key container '%s'\n",szUserName);
  41.     }
  42.  
  43.     // Attempt to get handle to signature key.
  44.     if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey)) {
  45.     if(GetLastError() == NTE_NO_KEY) {
  46.         // Create signature key pair.
  47.         printf("Create signature key pair\n");
  48.  
  49.         if(!CryptGenKey(hProv,AT_SIGNATURE,0,&hKey)) {
  50.         printf("Error %x during CryptGenKey!\n", GetLastError());
  51.         exit(1);
  52.         } else {
  53.         CryptDestroyKey(hKey);
  54.         }
  55.     } else {
  56.         printf("Error %x during CryptGetUserKey!\n", GetLastError());
  57.         exit(1);
  58.     }
  59.     }
  60.  
  61.     // Attempt to get handle to exchange key.
  62.     if(!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hKey)) {
  63.     if(GetLastError()==NTE_NO_KEY) {
  64.         // Create key exchange key pair.
  65.         printf("Create key exchange key pair\n");
  66.  
  67.         if(!CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hKey)) {
  68.         printf("Error %x during CryptGenKey!\n", GetLastError());
  69.         exit(1);
  70.         } else {
  71.         CryptDestroyKey(hKey);
  72.         }
  73.     } else {
  74.         printf("Error %x during CryptGetUserKey!\n", GetLastError());
  75.         exit(1);
  76.     }
  77.     }
  78.  
  79.     CryptReleaseContext(hProv,0);
  80.  
  81.     printf("OK\n");
  82.  
  83.     exit(0);
  84. }
  85.