daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ULONG DecryptMsg(PVOID pbEncryptedBlob, ULONG cbEncryptedBlob)
  2. {
  3.     if (HCERTSTORE hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"MY"))
  4.     {
  5.         CRYPT_DECRYPT_MESSAGE_PARA DecryptParams = {
  6.             sizeof(DecryptParams), X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 1, &hCertStore
  7.         };
  8.  
  9.         ULONG cb = cbEncryptedBlob;
  10.         ULONG dwError;
  11.         do
  12.         {
  13.             if (PVOID buf = LocalAlloc(0, cb))
  14.             {
  15.                 dwError = CryptDecryptMessage(&DecryptParams,
  16.                     (PBYTE)pbEncryptedBlob, cbEncryptedBlob,
  17.                     (PBYTE)buf, &cb, 0)
  18.                     ? NOERROR : GetLastError();
  19.  
  20.                 LocalFree(buf);
  21.             }
  22.             else
  23.             {
  24.                 dwError = GetLastError();
  25.             }
  26.         } while (dwError == ERROR_MORE_DATA);
  27.  
  28.         CertCloseStore(hCertStore, 0);
  29.  
  30.         return dwError;
  31.     }
  32.  
  33.     return GetLastError();
  34. }
  35.  
  36. void CryptMsg(PVOID pbPlain, ULONG cbPlain)
  37. {
  38.     if (HCERTSTORE hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"MY"))
  39.     {
  40.         if (PCCERT_CONTEXT pCertContext = GetCert(hCertStore))
  41.         {
  42.             CRYPT_ENCRYPT_MESSAGE_PARA EncryptParams = {
  43.                 sizeof(EncryptParams), PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0, { szOID_NIST_AES256_CBC }
  44.             };
  45.  
  46.             ULONG cbEncryptedBlob = cbPlain + pCertContext->cbCertEncoded;
  47.             ULONG dwError;
  48.  
  49.             do
  50.             {
  51.                 if (PVOID pbEncryptedBlob = LocalAlloc(0, cbEncryptedBlob))
  52.                 {
  53.                     dwError = CryptEncryptMessage(
  54.                         &EncryptParams,
  55.                         1, &pCertContext,
  56.                         (PBYTE)pbPlain, cbPlain,
  57.                         (PBYTE)pbEncryptedBlob, &cbEncryptedBlob)
  58.                         ? NOERROR : GetLastError();
  59.  
  60.                     if (dwError == NOERROR)
  61.                     {
  62.                         /****  write (pbEncryptedBlob, cbEncryptedBlob) to file  ****/;
  63.                         DecryptMsg(pbEncryptedBlob, cbEncryptedBlob);
  64.                     }
  65.  
  66.                     LocalFree(pbEncryptedBlob);
  67.                 }
  68.                 else
  69.                 {
  70.                     dwError = GetLastError();
  71.                 }
  72.  
  73.             } while (dwError == ERROR_MORE_DATA);
  74.            
  75.             CertFreeCertificateContext(pCertContext);
  76.         }
  77.         CertCloseStore(hCertStore, 0);
  78.     }
  79. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top