Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ULONG DecryptMsg(PVOID pbEncryptedBlob, ULONG cbEncryptedBlob)
- {
- if (HCERTSTORE hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"MY"))
- {
- CRYPT_DECRYPT_MESSAGE_PARA DecryptParams = {
- sizeof(DecryptParams), X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 1, &hCertStore
- };
- ULONG cb = cbEncryptedBlob;
- ULONG dwError;
- do
- {
- if (PVOID buf = LocalAlloc(0, cb))
- {
- dwError = CryptDecryptMessage(&DecryptParams,
- (PBYTE)pbEncryptedBlob, cbEncryptedBlob,
- (PBYTE)buf, &cb, 0)
- ? NOERROR : GetLastError();
- LocalFree(buf);
- }
- else
- {
- dwError = GetLastError();
- }
- } while (dwError == ERROR_MORE_DATA);
- CertCloseStore(hCertStore, 0);
- return dwError;
- }
- return GetLastError();
- }
- void CryptMsg(PVOID pbPlain, ULONG cbPlain)
- {
- if (HCERTSTORE hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"MY"))
- {
- if (PCCERT_CONTEXT pCertContext = GetCert(hCertStore))
- {
- CRYPT_ENCRYPT_MESSAGE_PARA EncryptParams = {
- sizeof(EncryptParams), PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0, { szOID_NIST_AES256_CBC }
- };
- ULONG cbEncryptedBlob = cbPlain + pCertContext->cbCertEncoded;
- ULONG dwError;
- do
- {
- if (PVOID pbEncryptedBlob = LocalAlloc(0, cbEncryptedBlob))
- {
- dwError = CryptEncryptMessage(
- &EncryptParams,
- 1, &pCertContext,
- (PBYTE)pbPlain, cbPlain,
- (PBYTE)pbEncryptedBlob, &cbEncryptedBlob)
- ? NOERROR : GetLastError();
- if (dwError == NOERROR)
- {
- /**** write (pbEncryptedBlob, cbEncryptedBlob) to file ****/;
- DecryptMsg(pbEncryptedBlob, cbEncryptedBlob);
- }
- LocalFree(pbEncryptedBlob);
- }
- else
- {
- dwError = GetLastError();
- }
- } while (dwError == ERROR_MORE_DATA);
- CertFreeCertificateContext(pCertContext);
- }
- CertCloseStore(hCertStore, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement