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 / enumalgs / enumalgs.c next >
C/C++ Source or Header  |  1997-10-08  |  3KB  |  93 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 <stdlib.h>
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include <windows.h>
  15. #include <wincrypt.h>
  16.  
  17. /*****************************************************************************/
  18. void _cdecl main(int argc, char *argv[])
  19. {
  20.     HCRYPTPROV hProv = 0;
  21.     BYTE *ptr = NULL;
  22.     DWORD i;
  23.     ALG_ID aiAlgid;
  24.     DWORD dwBits;
  25.     DWORD dwNameLen;
  26.     CHAR szName[100];          // Often allocated dynamically.
  27.     BYTE pbData[1000];          // Often allocated dynamically.
  28.     DWORD dwDataLen;
  29.     DWORD dwFlags;
  30.     CHAR *pszAlgType = NULL;
  31.  
  32.     // Get handle to the default provider.
  33.     if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
  34.         printf("Error %x during CryptAcquireContext!\n", GetLastError());
  35.         goto done;
  36.     }
  37.  
  38.     // Enumerate the supported algorithms.
  39.     for(i=0 ; ; i++) {
  40.     // Set the CRYPT_FIRST flag the first time through the loop.
  41.     if(i == 0) {
  42.         dwFlags = CRYPT_FIRST;
  43.     } else {
  44.         dwFlags = 0;
  45.     }
  46.  
  47.     // Retrieve information about an algorithm.
  48.     dwDataLen = 1000;
  49.     if(!CryptGetProvParam(hProv, PP_ENUMALGS, pbData, &dwDataLen, 0)) {
  50.         if(GetLastError() == ERROR_NO_MORE_ITEMS) {
  51.         // Exit the loop.
  52.         break;
  53.         } else {
  54.         printf("Error %x reading algorithm!\n", GetLastError());
  55.         return;
  56.         }
  57.     }
  58.  
  59.     // Extract algorithm information from 'pbData' buffer.
  60.     ptr = pbData;
  61.     aiAlgid = *(ALG_ID *)ptr;
  62.     ptr += sizeof(ALG_ID);
  63.     dwBits = *(DWORD *)ptr;
  64.     ptr += sizeof(DWORD);
  65.     dwNameLen = *(DWORD *)ptr;
  66.     ptr += sizeof(DWORD);
  67.     strncpy(szName, ptr,dwNameLen);
  68.  
  69.     // Determine algorithm type.
  70.     switch(GET_ALG_CLASS(aiAlgid)) {
  71.         case ALG_CLASS_DATA_ENCRYPT: pszAlgType = "Encrypt  ";
  72.                      break;
  73.         case ALG_CLASS_HASH:     pszAlgType = "Hash     ";
  74.                      break;
  75.         case ALG_CLASS_KEY_EXCHANGE: pszAlgType = "Exchange ";
  76.                      break;
  77.         case ALG_CLASS_SIGNATURE:     pszAlgType = "Signature";
  78.                      break;
  79.         default:             pszAlgType = "Unknown  ";
  80.     }
  81.  
  82.     // Print information about algorithm.
  83.     printf("Name:%-14s Type:%s  Bits:%-4d Algid:%8.8xh\n",
  84.         szName, pszAlgType, dwBits, aiAlgid
  85.     );
  86.     }
  87.  
  88.     done:
  89.  
  90.     // Release CSP handle.
  91.     if(hProv) CryptReleaseContext(hProv,0);
  92. }
  93.