Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Microsoft Crypto API contain high-level functions for asymmetric encryption and decryption with certificates. Look at <code>CryptEncryptessage</code> and <code>CryptDecryptMessage</code>.</p> <p>In decryption case your CERT_CONTEXT must have a <code>CERT_KEY_PROV_INFO_PROP_ID</code> property.</p> <p>I can give you an examples of usage:</p> <pre><code>const wchar_t message[] = L"This is a simple test message."; PCCERT_CONTEXT hCert = NULL; HCERTSTORE hStore = NULL; static bool openCertStoreMY(CDialog *parent) { if(!hStore) { hStore = CertOpenSystemStore(NULL, L"MY"); if(!hStore) { parent-&gt;MessageBox(L"Cannot open \"MY\"", L"Error", MB_ICONERROR); return false; } } return true; } void CTestDlg::OnEncryptClicked() { if(!hCert) { if(!openCertStoreMY(this)) return; hCert = CryptUIDlgSelectCertificateFromStore(hStore, GetSafeHwnd(), NULL, NULL, 0, 0, 0); if(!hCert) return; } CRYPT_ENCRYPT_MESSAGE_PARA params; memset(&amp;params, 0, sizeof(CRYPT_ENCRYPT_MESSAGE_PARA)); params.cbSize = sizeof(CRYPT_ENCRYPT_MESSAGE_PARA); params.dwMsgEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING; params.ContentEncryptionAlgorithm.pszObjId = "2.16.840.1.101.3.4.1.2"; //AES128 DWORD msz; DWORD cbMsg = sizeof(message); const BYTE *pbMsg = (PBYTE)message; if(!CryptEncryptMessage(&amp;params, 1, &amp;hCert, pbMsg, cbMsg, NULL, &amp;msz)) return; PBYTE outBuf = new BYTE[msz]; if(CryptEncryptMessage(&amp;params, 1, &amp;hCert, pbMsg, cbMsg, outBuf, &amp;msz)) { FILE *fil = _wfopen(filename, L"wb"); if(fil) { fwrite(outBuf, 1, msz, fil); fclose(fil); MessageBox(L"Complete"); } else MessageBox(L"Cannot open file", L"Error", MB_ICONERROR); } delete [] outBuf; } void CTestDlg::OnDecryptClicked() { if(!openCertStoreMY(this)) return; CRYPT_DECRYPT_MESSAGE_PARA params; params.cbSize = sizeof(CRYPT_DECRYPT_MESSAGE_PARA); params.dwMsgAndCertEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING; params.cCertStore = 1; params.rghCertStore = &amp;hStore; params.dwFlags = 0; DWORD cbMsg; PBYTE pbMsg; FILE *fil = _wfopen(filename, L"rb"); if(fil) { fseek(fil, 0 ,2); cbMsg = ftell(fil); fseek(fil, 0, 0); pbMsg = new BYTE[cbMsg]; fread(pbMsg, 1, cbMsg, fil); fclose(fil); } else { MessageBox(L"Cannot open file", L"Error", MB_ICONERROR); return; } DWORD msz; if(!CryptDecryptMessage(&amp;params, pbMsg, cbMsg, NULL, &amp;msz, NULL)) { delete [] pbMsg; return; } PBYTE outBuf = new BYTE[msz]; if(CryptDecryptMessage(&amp;params, pbMsg, cbMsg, outBuf, &amp;msz, NULL)) MessageBox((LPCWSTR)outBuf); delete [] pbMsg; delete [] outBuf; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload