DataFlex
DataFlex
openssl smime -encrypt -des3 -in <file> <pem file>
See more OpenSSL Examples
OpenSSL SMIME encrypt file using PEM containing a certificate.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPem
Variant vCert
Handle hoCert
Handle hoCrypt
Variant vBd
Handle hoBd
Handle hoSbEncryptedMime
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load the cert from a PEM file.
Get Create (RefClass(cComChilkatPem)) To hoPem
If (Not(IsComObjectCreated(hoPem))) Begin
Send CreateComObject of hoPem
End
// Our particular PEM was not encrypted, so we pass an empty password.
// Also, a private key is not needed for encryption. The PEM used to test this example
// happens to have a private key, but it's not actually used.
Get ComLoadPemFile Of hoPem "qa_data/openssl/rsaCertAndKey.pem" "" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPem To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComNumCerts Of hoPem To iTemp1
If (iTemp1 = 0) Begin
Showln "PEM does not contain any certificates."
Procedure_Return
End
Get ComGetCert Of hoPem 0 To vCert
If (IsComObject(vCert)) Begin
Get Create (RefClass(cComChilkatCert)) To hoCert
Set pvComObject Of hoCert To vCert
End
Get ComLastMethodSuccess Of hoPem To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoPem To sTemp1
Showln sTemp1
Procedure_Return
End
// -------------------------------------------------------------------------------------
// Duplicate this OpenSSL command: openssl smime -encrypt -des3 -in <file> <pem file>
// -------------------------------------------------------------------------------------
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Get ComSetEncryptCert Of hoCrypt vCert To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCrypt To sTemp1
Showln sTemp1
Send Destroy of hoCert
Procedure_Return
End
Send Destroy of hoCert
Set ComCryptAlgorithm Of hoCrypt To "PKI"
Set ComPkcs7CryptAlg Of hoCrypt To "3des"
Set ComKeyLength Of hoCrypt To 168
// Load the file to be encrypted.
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Get ComLoadFile Of hoBd "qa_data/openssl/hello.txt" To iSuccess
If (iSuccess = False) Begin
Showln "Failed to load the input file."
Procedure_Return
End
// Encrypt.
Get pvComObject of hoBd to vBd
Get ComEncryptBd Of hoCrypt vBd To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCrypt To sTemp1
Showln sTemp1
Procedure_Return
End
// The openssl smime -encrypt command produces encrypte MIME such as this:
// MIME-Version: 1.0
// Content-Disposition: attachment; filename="smime.p7m"
// Content-Type: application/x-pkcs7-mime; smime-type=enveloped-data; name="smime.p7m"
// Content-Transfer-Encoding: base64
//
// MIICzwYJKoZIhvcNAQcDoIICwDCCArwCAQAxggF8MIIBeAIBADBgMEoxCzAJBgNVBAYTAlVTMRYw
// FAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQDExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBY
// MwISA9Nqgb1dH/XTDNeKzN1nR85iMA0GCSqGSIb3DQEBAQUABIIBAIQqPexjxWovgxwKV/r3HL/U
// EP9Yozvz5hBeX5VvRZjKSi4FRw5wapElPK+4FB82hiAR9Mi3c16PvPSVkJv3l78Mv5uaaOs/OmUz
// mIHFB6Z+l2E52BDmUVWJZTQ09vdWy6+NIRlg2R9Z1NkmZ4BZCJk6mHB/Yx03IaOxK8LnwieDMthM
// SvxbhJnIOISN7k7ofs+/0vTXUpdQ+tlmwyVySMGQ6VMk+z4sqZJ2stacqCPtt/aiSwJ9p0OKmihf
// 3KDJceXJtavIQeA97yz1LqPvle35mmd5sBhV9qQYdTV/KJ+YM5uEZ9BHYbvMJbADFHwKzhcEY3qA
// 7T9acmEsb7NycOIwggE1BgkqhkiG9w0BBwEwFAYIKoZIhvcNAwcECERX/ZoHweSkgIIBEMmCMx49
// zjVAnGqRaBbvzQT1hg0uQSxIJjxMxC+HSuM+eY9oSOsbrw4uIijHKH9NdOpeDsdRzg2z5EBM7AlP
// Ht9DyPW5C2deV6RPX4F8gyExz+JUXrd+3Yb3AKTdpDkTWDmNCeO0r/YSqp518+mfU5hG8e336u51
// HAM44FeknA8oThWsD/wUB1e8vzsatK4UXW/KSu/166V7z+VT86kd+IHa7t60U9Yp0ZXgcM5Pb5Ni
// 69Qc5MKPzom2801H5UR/WjCgsxOIjOj49sKisjRy79skrJzxY5ZG05T0dKn6KC3TjRpIEEeOyhCd
// Nm2Y7dcW8GLMepdhWay5vePmQxmvmhbAtBprIem14NcrYeG6D5wP
// We have the body in bd
// Construct the header and base64 body...
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbEncryptedMime
If (Not(IsComObjectCreated(hoSbEncryptedMime))) Begin
Send CreateComObject of hoSbEncryptedMime
End
Get ComAppendLine Of hoSbEncryptedMime "MIME-Version: 1.0" True To iSuccess
Get ComAppendLine Of hoSbEncryptedMime 'Content-Disposition: attachment; filename="smime.p7m"' True To iSuccess
Get ComAppendLine Of hoSbEncryptedMime 'Content-Type: application/x-pkcs7-mime; smime-type=enveloped-data; name="smime.p7m"' True To iSuccess
Get ComAppendLine Of hoSbEncryptedMime "Content-Transfer-Encoding: base64" True To iSuccess
Get ComAppendLine Of hoSbEncryptedMime "" True To iSuccess
Get ComGetEncoded Of hoBd "base64_mime" To sTemp1
Get ComAppendLine Of hoSbEncryptedMime sTemp1 True To iSuccess
// Show the result.
Get ComGetAsString Of hoSbEncryptedMime To sTemp1
Showln sTemp1
// or save to a file..
Get ComWriteFile Of hoSbEncryptedMime "qa_output/encryptedMime.txt" "utf-8" False To iSuccess
End_Procedure