Advertisement
Shokedbrain

lab3 capi

Apr 11th, 2022
1,745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <wincrypt.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. // обработчик ошибок
  9. // для нормальной отладки кода
  10. void ErrorHandling(){
  11.     DWORD dw = GetLastError();
  12.     switch(dw){
  13.     case (DWORD)NTE_EXISTS:
  14.         cout << "NTE_EXISTS\n";
  15.         break;
  16.     case (DWORD)NTE_NO_KEY:
  17.         cout << "NTE_NO_KEY\n";
  18.         break;
  19.     case (DWORD)NTE_BAD_KEY:
  20.         cout << "NTE_BAD_KEY\n";
  21.         break;
  22.     case (DWORD)NTE_BAD_FLAGS:
  23.         cout << "NTE_BAD_FLAGS\n";
  24.         break;
  25.     case (DWORD)ERROR_INVALID_HANDLE:
  26.         cout << "ERROR_INVALID_HANDLE\n";
  27.         break;
  28.     case (DWORD)ERROR_INVALID_PARAMETER:
  29.         cout << "ERROR_INVALID_PARAMETER\n";
  30.         break;
  31.     }
  32. }
  33.  
  34. int main(){
  35.     HCRYPTPROV hCryptProv; // дескриптор криптопровайдера
  36.     LPCWSTR keyContainerName = L"Container";
  37.     // дескриптор ключей
  38.     HCRYPTKEY hCryptKey = NULL;
  39.     HCRYPTKEY toSignKey = NULL, toExportKey = NULL;
  40.     // создаём ключевой контейнер
  41.     if (!CryptAcquireContext(&hCryptProv,
  42.                             keyContainerName,
  43.                             MS_DEF_PROV,PROV_RSA_FULL,
  44.                             CRYPT_NEWKEYSET))
  45.     {
  46.         ErrorHandling();
  47.         return -1;
  48.     }
  49.     // генерируем ключи для цифровой подписи
  50.     if (!CryptGenKey(hCryptProv,AT_SIGNATURE,CRYPT_EXPORTABLE,&hCryptKey))
  51.     {
  52.         ErrorHandling();
  53.         goto CleanUp;
  54.     }
  55.  
  56.     if (!CryptGetUserKey(hCryptProv,AT_SIGNATURE,&toSignKey)){
  57.         ErrorHandling();
  58.         goto CleanUp;
  59.     }
  60.    
  61.     // генерируем ключи для обмена
  62.     if (!CryptGenKey(hCryptProv,AT_KEYEXCHANGE,CRYPT_EXPORTABLE,&hCryptKey))
  63.     {
  64.         ErrorHandling();
  65.         goto CleanUp;
  66.     }
  67.  
  68.     if (!CryptGetUserKey(hCryptProv,AT_KEYEXCHANGE,&toExportKey)){
  69.         ErrorHandling();
  70.         goto CleanUp;
  71.     }
  72.  
  73.     CleanUp:
  74.     if (hCryptProv)
  75.     {
  76.         // освобождаем память, удалив контейнер и
  77.         CryptAcquireContext(&hCryptProv,keyContainerName,
  78.                             MS_DEF_PROV,PROV_RSA_FULL,
  79.                             CRYPT_DELETEKEYSET);
  80.         CryptReleaseContext(hCryptProv,0);
  81.         cout << "Executed\n";
  82.     }
  83.     if (hCryptKey) // освобождаем память
  84.         CryptDestroyKey(hCryptKey);
  85.     if (toSignKey)
  86.         CryptDestroyKey(toSignKey);
  87.     if (toExportKey)
  88.         CryptDestroyKey(toExportKey);
  89.     system("pause");
  90.     return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement