Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) Sign and Encrypt using Cert to produce -----BEGIN PKCS7----- ... -----END PKCS7-----Demonstrates how to first sign content using your own cert + private key to produce PKCS7 binary output. Then encrypt the signed binary output using the recipient's public key to produce output such as: -----BEGIN PKCS7----- MIIHPwYJKoZIhvcNAQcEoIIHMDCCBywC ... ... ... -----END PKCS7-----
#include <CkCrypt2.h> #include <CkCert.h> #include <CkBinData.h> #include <CkStringBuilder.h> void ChilkatSample(void) { CkString strOut; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkCrypt2 crypt; const char *toBeSignedAndEncrypted = "This string is to be signed and then encrypted."; CkCert myCert; bool success = myCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123"); if (success != true) { strOut.append(myCert.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } success = crypt.SetSigningCert(myCert); if (success != true) { strOut.append(crypt.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Get the PKCS7 output in base64 format. crypt.put_EncodingMode("base64"); // Perhaps we don't want the entire cert chain to be included in the signature? crypt.put_IncludeCertChain(false); const char *signedPkcs7 = crypt.opaqueSignStringENC(toBeSignedAndEncrypted); // We're going to encrypt the binary PKCS7 signature (which contains the content) CkBinData bdPkcs7; bdPkcs7.AppendEncoded(signedPkcs7,"base64"); // --------------------------------------------------- // Now for the encryption... // Specify the encryption to be used. // "pki" indicates "Public Key Infrastructure" and will create a PKCS7 encrypted (enveloped) message. crypt.put_CryptAlgorithm("pki"); crypt.put_Pkcs7CryptAlg("aes"); crypt.put_KeyLength(128); crypt.put_OaepHash("sha256"); crypt.put_OaepPadding(true); // A certificate is needed as the encryption key.. CkCert cert; success = cert.LoadFromFile("qa_data/certs/testCert.pem"); if (success != true) { strOut.append(cert.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Tell the crypt object to use the certificate. crypt.SetEncryptCert(cert); success = crypt.EncryptBd(bdPkcs7); if (success != true) { strOut.append(crypt.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Make a "-----BEGIN PKCS7-----" ... "-----END PKCS7-----" sandwich... CkStringBuilder sb; sb.AppendLine("-----BEGIN PKCS7-----",true); sb.Append(bdPkcs7.getEncoded("base64_mime")); sb.AppendLine("-----END PKCS7-----",true); const char *outStr = sb.getAsString(); strOut.append(outStr); strOut.append("\r\n"); // Sample output: // -----BEGIN PKCS7----- // MIIMFQYJKoZIhvcNAQcDoIIMBjCCDAICAQAxggH5MIIB9QIBADCBsTCBmzELMAkGA1UEBhMCR0Ix // GzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMR // Q09NT0RPIENBIExpbWl0ZWQxQTA/BgNVBAMTOENPTU9ETyBTSEEtMjU2IENsaWVudCBBdXRoZW50 // aWNhdGlvbiBhbmQgU2VjdXJlIEVtYWlsIENBAhEAuBl4qE2MODB05C5h53M5UDA4BgkqhkiG9w0B // AQcwK6APMA0GCWCGSAFlAwQCAQUAoRgwFgYJKoZIhvcNAQEIMAkGBSsOAwIaBQAEggEAA34hkvxY // TXMUAsWBmnRIvfFK920WeUVenM4ob7qibDiaXI2qWBgzkIfQH2T1vIYHHzNQuaaiTrK3THn8Nzfy // RFqTnhfWwsIL/yinHVn5rA9CbHFmxqHr2UctZxLB7x/DxV7pjqpTTveCwgC9FykxGBcNO92Thlho // bH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ZUscah/9iVte5xGnsCOSq18PjPIdqMvzSEcrYQVyqe2/68mae6dpk7gyeiTYpDsSYO+KaHw // bwwViPnVOhRkXxU54unymiSmgIRrhn+hsbkxvJk3Q307a7wcj+/ygw5stTV2hLmIQotMvmlpFTLZ // PhB5eyFIZaWKV8XJgPV5JInrm39taw8ClHeSC31BNgfkX5u2h0pA1f5zMZ/SvgriW0Xumyoy/9XC // eVL6fCZEWmaRZbAy5RlVELR/N2/Z5VS6s8U6Wrck9Zq9sGelf+0QWbCEwNoxQf+xxH55QnzHRvpu // s839qRy/5msXSYno0fl3uE6cP/bWwk0Rvt3QYp75UTZDkfXl0ywy00V6AMPssKlDZe0GYWaqcHqz // xj2sOK++Iz0tlmhS6QWcGrgWyVaHG9E2van/yLE4Sl4qnKt88/vK+cTKCrRCStslrt2Z/1myLnaB // 6b6uAlVFk+kIHH82WaXxfCVedoGVk+jL3qPRbQiCzm7O66N6ecXIFWQ10sR5AXk/k4M6rM2Zhgoh // yjPAxEHOzZ7nTLMb2MvPSV4KbrQT7Qs8prefY6o1OANXu4HRzL+zzttiIrV72vVemF80q7e5wYKR // pGC85zZwz084RSWSwHTXDeWpI8zRPM7eboR97q9dfy7V23YlYJyM2RYrusweULib6X4me9nPhOzD // 4KmqI6FSOvwTUdEDb8k32+GVB+zXsEhI6uPFz+Fgve8WUKcWX0DFoD905+KICeDFlOxWjB5FhKqF // DysgV1FCCN8fGWRvbe7R74KR8SYwUOSAE63PDtGDWK345sOQXp5b0Vv4lVG5/ECZqYCM9RixLsyx // vrozd8L0wqQJ5vlMOt9wksSYa8zzShC/tO3aGyw9NLz6y3lKoz3NfA04k8IyT2wtx2cG82h4bdwo // aBPdFlV9P/WlVPJXgRv0GFJFX4HiVMQBEn3Rj8H/BHzZYPo9tLnPRmuoTWv+HaoAEzxwQ4O+L9av // 0BO0eJSJNxs3UTaxl/psm17EinNLCV7lx7jKppj8v7Xxdem0+Fu0ME92XmFKG52tT9XkniF6orNP // 9G48f93G7tIx4CLNT5QXhnUejZG0v0uBxnL5dURV4W/49Qg49lEZZr6yUZlDUhMBSKwCW/gcTeGv // XVscW4UaGH9ewPFYVHNaDh7ehxkaTtuTrVpS7fPhaWLwozR1ro+U/WIqjE7QeharK+eqJN7OJNcV // vx42zsCCzAankT70bGsEqtA/il4IbxEa3EQTqOMLdxSJ/62IZRKGNxwZnRou8i1pswMM5q/9/ITi // MweZHH+AWeXrf5CLVObq2ZPk6NUPScFKSy7Vao1SxORhj+YJDd3vznWpjjq88Qs1ntwitY13jnxh // 58XEwI+vkjAY34lOEtr2UGXS0DrANN0//VMk40WZat5rVnx9XITC9RmGFvtM0RDgMZ3BOEXyI1mI // nGLILcAVW+mnxJcafwFCbI1nd6KyfQU+/RmXdOLPtKMjEKm2jBZt3UGaxLkHKCuzESdYtlUkbbcL // K5ynNEANfBNJjf494Sd+jbKe9031E7bl4nbQGFpmJDEnszWfKMdg9ChlOfYLM26pFN0hQahA26fn // PgrBYvKaHauR0SUPtibwmy2kIdKoakJoIvyDDYh6HHrdcwGVHfA5nCSFIHGsv3lcdLc2sMq38TR/ // zh7verXj0Aa71VFfmxBrRvasvdp9ms7GZFzDzMq2yf+a4knzTdRHtst6jOoTXb0RXJuLfvDTJqmF // tue95J8OELZOpWnp21Yj52So78BlcOC4W3VexT4hyHhtziG9BKeXvc6WSU1pUEoNkXAXXrHLi/AG // lC45zJ+LzDp/3rOzGQ5SIIi67ZSSQNiW85ektAoSEJZlZ1xaXt9DEbugpkySfjI/GFxSmFZqCs6h // RNT4f8BAKX56ItobBORZbvk12Mn5QrxIEGFbOFcLt6cA/zqixS8ej1cz+SsMUkuzl/KDh6ncXeuF // 2DawyL7/konWins6Jo3gYhRgw4fpck14aaEF5/+h3FvgVw23FieWYc6AImn/2zvtv6+gCJl7YnrG // 9zSjHx2Kf7WRiFbpSqYXGGA0iD0b7So9oyrTItepIRQh0OFcDhCGXvhl2Sb7IQM7sL3WvbAHcseS // 0xJiEIZogWbVKL5OqAn/VrJsL+0atvkh6qy5FVmk9CBWh92lkmwWyk8+CoTVC6DPMRXLv/MQar2W // j0cOL12vohVJe9MSHTMR6kHaaxj1J8b5AswZTBsbgFo16Q6ihxLC1fd70OM1c6Hak341UgCxHsOr // j9JfT+r5jpK3booIzBorGNJOkAuaBAEKdcnNpyCP2sDCzG+7Ws1H+lJzXVDXARftZSbMvfLctU49 // 5YZl+bBifBF8fSSSRNKXMbPeyww1idjezWKiXQC3hA6VPKtW7rn1py7hTlkdm6os1jW9vmSmz30j // KHvey6Kcy+fdc1gyYmXhSDD55uYVrj1JmbaZvIeGTh8cQ8E4Dsq2Hy2PQ8S2e2S6YcJ9X6yNkwXG // KrqG2vK4AeBuro1IfoUG1EJ17Q== // -----END PKCS7----- SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.