Advertisement
Guest User

Untitled

a guest
Aug 17th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. *****************************
  2. <original text>
  3. 6b c1 be e2 2e 40 9f 96  e9 3d 7e 11 73 93 17 2a
  4. </original text>
  5. <encrypted>
  6. 76 49 ab ac 81 19 b2 46  ce e9 8e 9b 12 e9 19 7d
  7. 89 64 e0 b1 49 c1 0b 7b  68 2e 6e 39 aa eb 73 1c
  8. </encrypted>
  9. <plain text>
  10. 6b c1 be e2 2e 40 9f 96  e9 3d 7e 11 73 93 17 2a
  11. </plain text>
  12. *****************************
  13. <original text>
  14. ae 2d 8a 57 1e 03 ac 9c  9e b7 6f ac 45 af 8e 51
  15. </original text>
  16. <encrypted>
  17. 50 86 cb 9b 50 72 19 ee  95 db 11 3a 91 76 78 b2
  18. 55 e2 1d 71 00 b9 88 ff  ec 32 fe ea fa f2 35 38
  19. </encrypted>
  20. <plain text>
  21. ae 2d 8a 57 1e 03 ac 9c  9e b7 6f ac 45 af 8e 51
  22. </plain text>
  23.  
  24. void DumpBytes(const BYTE * pb, ULONG cb, PCSTR msg)
  25. {
  26.     PSTR pszString = 0;
  27.     DWORD cchString = 0;
  28.  
  29.     while (CryptBinaryToStringA(pb, cb, CRYPT_STRING_HEX, pszString, &cchString))
  30.     {
  31.         if (pszString)
  32.         {
  33.             DbgPrint("<%s>\n", msg);
  34.            
  35.             do
  36.             {
  37.                 DbgPrint("%.*s", cb = min(0x100, cchString), pszString);
  38.             } while (pszString += cb, cchString -= cb);
  39.            
  40.             DbgPrint("</%s>\n", msg);
  41.            
  42.             return;
  43.         }
  44.  
  45.         pszString = (PSTR)alloca(cchString);
  46.     }
  47. }
  48.  
  49. void aes_dec(const void* pvBlob, ULONG cbBlob, const BYTE* Iv, PBYTE pbEnc, ULONG cbEnc)
  50. {
  51.     HCRYPTPROV hProv;
  52.     if (CryptAcquireContextW(&hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
  53.     {
  54.         HCRYPTKEY hKey;
  55.         if (CryptImportKey(hProv, (PBYTE)pvBlob, cbBlob, 0, CRYPT_EXPORTABLE, &hKey))
  56.         {
  57.             DWORD dwAesMode = CRYPT_MODE_CBC;
  58.             if (CryptSetKeyParam(hKey, KP_MODE, (PBYTE)&dwAesMode, 0) &&
  59.                 CryptSetKeyParam(hKey, KP_IV, Iv, 0) &&
  60.                 CryptDecrypt(hKey, 0, TRUE, 0, pbEnc, &cbEnc))
  61.             {
  62.                 DumpBytes(pbEnc, cbEnc, "plain text");
  63.             }
  64.  
  65.             CryptDestroyKey(hKey);
  66.         }
  67.  
  68.         CryptReleaseContext(hProv, 0);
  69.     }
  70. }
  71.  
  72. void aes_enc(const void* pvBlob, ULONG cbBlob, const BYTE* Iv, const void* pvTxt, ULONG cbTxt)
  73. {
  74.     DbgPrint("*****************************\n");
  75.     DumpBytes((PBYTE)pvTxt, cbTxt, "original text");
  76.  
  77.     HCRYPTPROV hProv;
  78.     if (CryptAcquireContextW(&hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
  79.     {
  80.         HCRYPTKEY hKey;
  81.         if (CryptImportKey(hProv, (PBYTE)pvBlob, cbBlob, 0, CRYPT_EXPORTABLE, &hKey))
  82.         {
  83.             DWORD dwAesMode = CRYPT_MODE_CBC;
  84.             if (CryptSetKeyParam(hKey, KP_MODE, (PBYTE)&dwAesMode, 0) &&
  85.                 CryptSetKeyParam(hKey, KP_IV, Iv, 0))
  86.             {
  87.                 BYTE* pbData = 0;
  88.                 ULONG dwDataLen, dwBufLen = 0;
  89.                 while (CryptEncrypt(hKey, 0, TRUE, 0, pbData, &(dwDataLen = cbTxt), dwBufLen))
  90.                 {
  91.                     if (pbData)
  92.                     {
  93.                         DumpBytes(pbData, dwDataLen, "encrypted");
  94.  
  95.                         aes_dec(pvBlob, cbBlob, Iv, pbData, dwDataLen);
  96.                         break;
  97.                     }
  98.  
  99.                     memcpy(pbData = (PBYTE)alloca(dwBufLen = dwDataLen), pvTxt, cbTxt);
  100.                 }
  101.             }
  102.  
  103.             CryptDestroyKey(hKey);
  104.         }
  105.  
  106.         CryptReleaseContext(hProv, 0);
  107.     }
  108. }
  109.  
  110. void aes_demo()
  111. {
  112.     struct CRYPTOAPI_AES128_KEYBLOB : BLOBHEADER {
  113.         DWORD dwKeySize;
  114.         BYTE ucKeyBytes[16];
  115.     };
  116.  
  117.     static const CRYPTOAPI_AES128_KEYBLOB blob = {
  118.         { PLAINTEXTKEYBLOB, CUR_BLOB_VERSION, 0, CALG_AES_128 }, sizeof(blob.ucKeyBytes),
  119.         { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }
  120.     };
  121.  
  122.     static const BYTE ucAes128Test1Iv[] =  {
  123.         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  124.     };
  125.  
  126.     static const BYTE ucAes128Test2Iv[] =  {
  127.         0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d
  128.     };
  129.  
  130.     static const BYTE ucAes128Test1Ptx[] = {
  131.         0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  132.     };
  133.  
  134.     static const BYTE ucAes128Test2Ptx[] = {
  135.         0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  136.     };
  137.  
  138.     aes_enc(&blob, sizeof(blob), ucAes128Test1Iv, ucAes128Test1Ptx, sizeof(ucAes128Test1Ptx));
  139.     aes_enc(&blob, sizeof(blob), ucAes128Test2Iv, ucAes128Test2Ptx, sizeof(ucAes128Test2Ptx));
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement