Advertisement
Shokedbrain

hybrid lab4

May 17th, 2022
2,244
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. void ErrorHandling(){
  9.     DWORD dw = GetLastError();
  10.     cout << "Error: " << hex << dw << endl;
  11. }
  12.  
  13. int hybridED(){
  14.     string          data = "Hello, World!"; // входная строка
  15.     string          encryptedData = data;
  16.     DWORD           dataLength = static_cast<DWORD>(data.length()); // её длина
  17.     HCRYPTPROV      hProv;
  18.     LPCWSTR         hName = L"Container"; // имя контейнера
  19.     HCRYPTKEY       hSessionKey, hExportKey; // ключ сессии и публичный
  20.     HCRYPTKEY       hImportKey; // ключ для импорта
  21.     DWORD           pbDataLen = 0; // длина массива для экспорта ключа
  22.     if (!CryptAcquireContext(&hProv,hName,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET))
  23.     {
  24.         ErrorHandling();
  25.         return -1;
  26.     }
  27.     // генерируем ключ сессии
  28.     cout << "Generating session key\n";
  29.     if (!CryptGenKey(hProv,CALG_RC4,
  30.         CRYPT_EXPORTABLE | CRYPT_ENCRYPT | CRYPT_DECRYPT,
  31.         &hSessionKey))
  32.     {
  33.         ErrorHandling();
  34.         return -1;
  35.     }
  36.     cout << "Session key has been generated\n";
  37.     // генерируем ключ для экспорта в данном контексте
  38.     cout << "Generating export key\n";
  39.     if (!CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hExportKey))
  40.     {
  41.         ErrorHandling();
  42.         return -1;
  43.     }
  44.     if (!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hExportKey))
  45.     {
  46.         ErrorHandling();
  47.         return -1;
  48.     }
  49.     cout << "Export key has been generated\n";
  50.     // рассчитываем длину для экспорта ключа
  51.     if (!CryptExportKey(hSessionKey,hExportKey,SIMPLEBLOB,0,NULL,&pbDataLen))
  52.     {
  53.         ErrorHandling();
  54.         return -1;
  55.     }
  56.  
  57.     PBYTE exportKey = static_cast<PBYTE>(malloc(pbDataLen));
  58.     ZeroMemory(exportKey,pbDataLen);
  59.     // Экспортируем ключ шифрования
  60.     if (!CryptExportKey(hSessionKey,hExportKey,SIMPLEBLOB,0, exportKey,&pbDataLen))
  61.     {
  62.         ErrorHandling();
  63.         return -1;
  64.     }
  65.     cout << "Key has been exported\n";
  66.  
  67.     cout << "Data encryption\n";
  68.     cout << "\t\tInput data\n";
  69.     cout << "Data: " << data << endl;
  70.     cout << "Data length: " << data.length() << endl;
  71.     if (!CryptEncrypt(hSessionKey,0,TRUE,0,
  72.         (PBYTE) encryptedData.c_str(),&dataLength,data.length()))
  73.     {
  74.         ErrorHandling();
  75.         return -1;
  76.     }
  77.     else
  78.     {
  79.         cout << "Encrypted successfully\n";
  80.         cout << "Encrypted string: " << encryptedData << endl;
  81.         cout << "Encrypted data length:" << static_cast<int>(dataLength) << endl;
  82.     }
  83.    
  84.     cout << "Data decryption\n";
  85.  
  86.     cout << "Importing key\n";
  87.     if (!CryptImportKey(hProv,exportKey,pbDataLen,hExportKey,0,&hImportKey))
  88.     {
  89.         ErrorHandling();   
  90.         return -1;
  91.     }
  92.  
  93.     cout << "Key has been imported successfully\n";
  94.  
  95.     string decryptedData = encryptedData;
  96.  
  97.     if (CryptDecrypt(hImportKey,0,TRUE,0,(BYTE *) decryptedData.c_str(),&dataLength))
  98.     {
  99.         cout << "Decrypted successfully\n";
  100.         cout << "Decrypted string: " << decryptedData << endl;
  101.         cout << "Decrypted data length: " << static_cast<int>(dataLength) << endl;
  102.     }
  103.     else
  104.     {
  105.         ErrorHandling();
  106.         return -1;
  107.     }
  108.     // освобождаем память
  109.     if (hProv)
  110.     {
  111.         CryptAcquireContext(&hProv,hName,NULL,PROV_RSA_FULL,CRYPT_DELETEKEYSET);
  112.         CryptReleaseContext(hProv,0);
  113.     }
  114.     if (hSessionKey)
  115.         CryptDestroyKey(hSessionKey);
  116.     if (hExportKey)
  117.         CryptDestroyKey(hExportKey);
  118.     return 0; // succeded
  119. }
  120.  
  121.  
  122. int main(){
  123.     cout << "\t\tHYDRID Encryption\n";
  124.     hybridED();
  125.     system("pause");
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement