istomina_sofia

л3с++cryptapi

Mar 26th, 2024
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <wincrypt.h>
  4. #include <string>
  5. #include <iomanip>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     setlocale(LC_ALL, "RUS");
  13.  
  14.     string nameC;
  15.     cout << "Введите имя контейнера: ";
  16.     cin >> nameC;
  17.  
  18.     HCRYPTKEY hKey;
  19.     HCRYPTPROV hProv;
  20.     LPCSTR Container = nameC.c_str();
  21.  
  22.  
  23.     if (CryptAcquireContextA(&hProv, Container, NULL, PROV_RSA_FULL, 0))
  24.     {
  25.         cout << "Контейнер " << Container << " существует" << endl;
  26.  
  27.     jump:
  28.  
  29.         if (CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hKey))
  30.         {
  31.             cout << "Ключ извлечен" << endl;
  32.             CryptDestroyKey(hKey);
  33.         }
  34.  
  35.         else
  36.         {
  37.             if (CryptGenKey(hProv, CALG_RSA_KEYX, CRYPT_EXPORTABLE, &hKey))
  38.             {
  39.                 cout << "Ключ создан" << endl;
  40.                 CryptDestroyKey(hKey);
  41.             }
  42.  
  43.             else
  44.             {
  45.                 cerr << "Ошибка при создании ключа: " << GetLastError();
  46.                 if (hKey) CryptDestroyKey(hKey);
  47.             }
  48.         }
  49.     }
  50.  
  51.     else
  52.     {
  53.         if (CryptAcquireContextA(&hProv, Container, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
  54.         {
  55.             cout << "Контейнер " << Container << " был создан" << endl;
  56.  
  57.             goto jump;
  58.         }
  59.  
  60.         else
  61.         {
  62.             cerr << "Ошибка при создании контейнера: " << GetLastError();
  63.         }
  64.     }
  65.  
  66.     CryptReleaseContext(hProv, 0);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment