Advertisement
Shokedbrain

CryptEnumProviders Example

Feb 16th, 2022
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #pragma comment(lib, "crypt32.lib")
  2.  
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <windows.h>
  6. #include <wincrypt.h>
  7. #include <iostream>
  8.  
  9. void MyHandleError(TCHAR* s);
  10. void Wait(TCHAR* s);
  11.  
  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14.     // Declare and initialize variables.
  15.     LPTSTR pszName;
  16.     DWORD dwType;
  17.     DWORD cbName;
  18.     DWORD dwIndex = 0;
  19.     LPTSTR pbProvName;
  20.     DWORD cbProvName;
  21.  
  22.     //--------------------------------------------------------------
  23.     // Print header lines for providers.
  24.     _tprintf(TEXT("\n\nListing Available Providers.\n"));
  25.     _tprintf(TEXT("Provider type    Provider Name\n"));
  26.     _tprintf(TEXT("_____________    ")
  27.         TEXT("_____________________________________\n"));
  28.  
  29.     //---------------------------------------------------------------
  30.     // Loop through enumerating providers.
  31.     dwIndex = 0;
  32.     while (CryptEnumProviders(
  33.         dwIndex,
  34.         nullptr,
  35.         0,
  36.         &dwType,
  37.         nullptr,
  38.         &cbName))
  39.     {
  40.         //-----------------------------------------------------------
  41.         // cbName is the length of the name of the next provider.
  42.         // Allocate memory in a buffer to retrieve that name.
  43.         if (!(pszName = static_cast<LPTSTR>(LocalAlloc(LMEM_ZEROINIT, cbName))))
  44.         {
  45.             //MyHandleError(TEXT("ERROR - LocalAlloc failed!"));
  46.             _tprintf(TEXT("ccc.\n"));
  47.         }
  48.  
  49.         //-----------------------------------------------------------
  50.         // Get the provider name.
  51.         if (CryptEnumProviders(
  52.             dwIndex++,
  53.             nullptr,
  54.             0,
  55.             &dwType,
  56.             pszName,
  57.             &cbName))
  58.         {
  59.             /* _tprintf(TEXT("     %4.0d        %s\n"),
  60.                  dwType,
  61.                  pszName);*/
  62.             std::wcout << dwType << " " << pszName << "\n";
  63.         }
  64.         else
  65.         {
  66.             ///MyHandleError(TEXT("ERROR - CryptEnumProviders"));
  67.             _tprintf(TEXT("ddd.\n"));
  68.         }
  69.  
  70.         LocalFree(pszName);
  71.     } // End while loop.
  72.  
  73.     //---------------------------------------------------------------
  74.     // Get the name of the default CSP specified for the
  75.     // PROV_RSA_FULL type for the computer.
  76. } // End main.
  77.  
  78.  
  79. void MyHandleError(TCHAR* s)
  80. {
  81.     _tprintf(TEXT("An error occurred in running the program.\n"));
  82.     _tprintf(TEXT("%s\n"), s);
  83.     _tprintf(TEXT("Error number %x\n."), GetLastError());
  84.     _tprintf(TEXT("Program terminating.\n"));
  85.     exit(1);
  86. }
  87.  
  88. void Wait(TCHAR* s)
  89. {
  90.     char x;
  91.     _tprintf(s);
  92.     x = getchar();
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement