Advertisement
Shokedbrain

crypto lab 2

Feb 26th, 2022
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. #include <wincrypt.h>
  5.  
  6. using namespace std;
  7.  
  8. // вывод ошибок
  9. void ErrorHandling(string message)
  10. {
  11.     DWORD dw = GetLastError();
  12.     cout << "Error with: " << message << "\n";
  13.     cout << "Error code: " << dw << "\n";
  14. }
  15.  
  16. int main()
  17. {
  18.     HCRYPTPROV  hCryptProv;
  19.     HCRYPTHASH  hHash;
  20.     HCRYPTHASH  hDuplicatedHash;
  21.     if (CryptAcquireContext(
  22.         &hCryptProv,
  23.         nullptr,
  24.         nullptr,
  25.         PROV_RSA_FULL,
  26.         0))
  27.     {
  28.         cout << "CryptAcquireContext complete. \n";
  29.     }
  30.     else
  31.     {
  32.         ErrorHandling("Acquisition of context failed.");
  33.     }
  34.  
  35.     // получить дескриптор хэш объекта
  36.  
  37.     if (CryptCreateHash(
  38.         hCryptProv,
  39.         CALG_MD5,
  40.         0,
  41.         0,
  42.         &hHash))
  43.     {
  44.         cout << "An empty hash object has been created. \n";
  45.     }
  46.     else
  47.     {
  48.         ErrorHandling("CryptBeginHash");
  49.     }
  50.  
  51.     string input = "qwerty";
  52.  
  53.     if (CryptHashData(
  54.         hHash, reinterpret_cast<const BYTE*>(input.c_str()), input.size(),0
  55.     ))
  56.     {
  57.         cout << "CryptHashData executed successfully\n";
  58.     }
  59.     else
  60.     {
  61.         ErrorHandling("CryptHashData");
  62.     }
  63.  
  64.    
  65.  
  66.     if (CryptDuplicateHash(
  67.         hHash,
  68.         nullptr,
  69.         0,
  70.         &hDuplicatedHash
  71.     ))
  72.     {
  73.         cout << "The hash has been duplicated. \n";
  74.     }
  75.     else
  76.     {
  77.         ErrorHandling("CryptDuplicateHash");
  78.     }
  79.  
  80.     BYTE*        pbHash;
  81.     DWORD        dwHashLen;
  82.     DWORD        dwHashLenSize = sizeof(DWORD);
  83.  
  84.     if (CryptGetHashParam(
  85.         hDuplicatedHash,
  86.         HP_HASHSIZE,
  87.         reinterpret_cast<BYTE*>(&dwHashLen),
  88.         &dwHashLenSize,
  89.         0
  90.     ))
  91.     {
  92.         cout << "CryptGetHashParam HP_HASHSIZE executed successfully\n";
  93.     }
  94.     else
  95.     {
  96.         ErrorHandling("CryptGetHashParam failed to get size.");
  97.        
  98.     }
  99.  
  100.     if ((pbHash = static_cast<BYTE*>(malloc(dwHashLen))))
  101.         cout << "Allocation pbHash success\n";
  102.     else
  103.         cout << "Allocation failed\n";
  104.  
  105.  
  106.     string res;
  107.     if (CryptGetHashParam(
  108.         hHash,
  109.         HP_HASHVAL,
  110.         pbHash,
  111.         &dwHashLen,
  112.         0))
  113.     {
  114.         if (pbHash == nullptr)
  115.         {
  116.             cout << "system error\n";
  117.             exit(1);
  118.         }
  119.         // первый вариант вывода
  120.         cout << "1 hash is: ";
  121.         for (size_t i = 0; i!= 16; ++i)
  122.         {
  123.             res += "0123456789ABCDEF"[pbHash[i] / 16];
  124.             res += "0123456789ABCDEF"[pbHash[i] % 16];
  125.         }
  126.         cout << res << "\n";
  127.         // второй вариант вывода
  128.         cout << "2 hash is: ";
  129.         for(int i = 0 ; i < dwHashLen ; i++)
  130.         {
  131.            printf("%02x",pbHash[i]);
  132.         }
  133.         cout << "\n";
  134.     }
  135.     else
  136.     {
  137.         ErrorHandling("CryptGetHashParam\n");
  138.     }
  139.  
  140.     if (hHash)
  141.         CryptDestroyHash(hHash);
  142.     if (hDuplicatedHash)
  143.         CryptDestroyHash(hDuplicatedHash);
  144.     if (hCryptProv)
  145.         CryptReleaseContext(hCryptProv, 0);
  146.    
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement