Advertisement
Shokedbrain

asdsaddsfsf

May 17th, 2022
2,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fstream>
  2. #include <iomanip>
  3. #include <Windows.h>
  4. #include <wincrypt.h>
  5. #include <iostream>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. void ErrorHandling() {
  11.     DWORD dw = GetLastError();
  12.     cout << "Error: " << hex << dw << endl;
  13. }
  14.  
  15. string ToHex(const string& s, bool upper_case /* = true */)
  16. {
  17.     ostringstream ret;
  18.  
  19.     for (string::size_type i = 0; i < s.length(); ++i)
  20.         ret << std::hex << std::setfill('0') << std::setw(2) << (upper_case ? std::uppercase : std::nouppercase) << (int)s[i];
  21.  
  22.     return ret.str();
  23. }
  24.  
  25.  
  26. int main()
  27. {
  28.     //  var section
  29.     cout << "enter string: ";
  30.     string data{}; // входная строка
  31.     cin >> data;
  32.     string          encryptedData = data;
  33.     DWORD           dataLength = static_cast<DWORD>(data.length()); // её длина
  34.     HCRYPTPROV      hProv;
  35.     LPCWSTR         hName = L"QAUontainer"; // имя контейнера
  36.     HCRYPTKEY       hSessionKey, hExportKey; // ключ сессии и публичный
  37.     HCRYPTKEY       hImportKey; // ключ для импорта
  38.     DWORD           pbDataLen = 0; // длина массива для экспорта ключа
  39.  
  40.  
  41.     cout << "Creating container...\n";
  42.     // создаём контейнер
  43.     if (!CryptAcquireContext(&hProv, hName, nullptr,
  44.         PROV_RSA_FULL, CRYPT_NEWKEYSET))
  45.     {
  46.         ErrorHandling();
  47.         return -1;
  48.     }
  49.     cout << "Container successfully generated!\n";
  50.  
  51.     // генерируем ключ сессии
  52.     cout << "Generating session key...\n";
  53.     if (!CryptGenKey(hProv, CALG_RC4,
  54.         CRYPT_EXPORTABLE | CRYPT_ENCRYPT | CRYPT_DECRYPT,
  55.         &hSessionKey))
  56.     {
  57.         ErrorHandling();
  58.         return -1;
  59.     }
  60.     cout << "Session key has been generated\n";
  61.  
  62.     // генерируем ключ для экспорта в данном контексте
  63.     cout << "Generating export key...\n";
  64.     if (!CryptGenKey(hProv, AT_KEYEXCHANGE, 0, &hExportKey))
  65.     {
  66.         ErrorHandling();
  67.         return -1;
  68.     }
  69.     // дескриптор открытого ключа
  70.     if (!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hExportKey))
  71.     {
  72.         ErrorHandling();
  73.         return -1;
  74.     }
  75.  
  76.     cout << "Export key has been generated\n";
  77.  
  78.     // рассчитываем длину для экспорта ключа
  79.     if (!CryptExportKey(hSessionKey, hExportKey, SIMPLEBLOB, 0, NULL, &pbDataLen))
  80.     {
  81.         ErrorHandling();
  82.         return -1;
  83.     }
  84.     BYTE* exportKey = static_cast<BYTE*>(malloc(pbDataLen));
  85.     ZeroMemory(exportKey, pbDataLen);
  86.  
  87.     // Экспортируем ключ шифрования
  88.     if (!CryptExportKey(hSessionKey, hExportKey, SIMPLEBLOB, 0, exportKey, &pbDataLen))
  89.     {
  90.         ErrorHandling();
  91.         return -1;
  92.     }
  93.     cout << "Key has been exported\n";
  94.     // вывод ключа в 16-ричном виде
  95.     cout << "export key: ";
  96.     stringstream ss;
  97.     ss << hex << setfill('0');
  98.     for (int i = 0; i < pbDataLen; i++)
  99.         ss << setw(2) << static_cast<unsigned>(exportKey[i]);
  100.     cout << ss.str() << endl;
  101.  
  102.  
  103.     cout << "Data encryption\n";
  104.     cout << "\t\tInput data\n";
  105.     cout << "Data: " << data << endl;
  106.     cout << "Data length: " << data.length() << endl;
  107.     if (!CryptEncrypt(hSessionKey, 0, TRUE, 0,
  108.         (PBYTE)encryptedData.c_str(), &dataLength, data.length()))
  109.     {
  110.         ErrorHandling();
  111.         return -1;
  112.     }
  113.     cout << "Encrypted successfully\n";
  114.     cout << "Encrypted string: " << ToHex(encryptedData, false) << endl;
  115.     cout << "Encrypted data length:" << static_cast<int>(dataLength) << endl;
  116.  
  117.  
  118.     cout << "Data decryption\n";
  119.  
  120.     cout << "Importing key\n";
  121.     if (!CryptImportKey(hProv, exportKey, pbDataLen, hExportKey, 0, &hImportKey))
  122.     {
  123.         ErrorHandling();
  124.         return -1;
  125.     }
  126.  
  127.     cout << "Key has been imported successfully\n";
  128.  
  129.     string decryptedData = encryptedData;
  130.     if (CryptDecrypt(hImportKey, 0, TRUE, 0, (BYTE*)decryptedData.c_str(), &dataLength))
  131.     {
  132.         cout << "Decrypted successfully\n";
  133.         cout << "Decrypted string: " << decryptedData << endl;
  134.         cout << "Decrypted data length: " << static_cast<int>(dataLength) << endl;
  135.     }
  136.     else
  137.     {
  138.         ErrorHandling();
  139.         return -1;
  140.     }
  141.  
  142.  
  143.     // освобождаем память
  144.     if (hProv)
  145.     {
  146.         CryptAcquireContext(&hProv, hName, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
  147.         CryptReleaseContext(hProv, 0);
  148.     }
  149.     if (hSessionKey)
  150.         CryptDestroyKey(hSessionKey);
  151.     if (hExportKey)
  152.         CryptDestroyKey(hExportKey);
  153.     system("pause");
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement