Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PureBasic Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(PureBasic) PKCS7 Encrypt using Multiple Certificates

Encrypts MIME using multiple digital certificates. Calls AddEncryptCert once per certificate, followed by a call to EncryptN. The MIME may be decrypted by any owner of one of the certificates + its associated private key.

Chilkat PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkCert.pb"
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Build a simple MIME message to be encrypted:
    success.i = CkMime::ckAddHeaderField(mime,"Content-Type","text/plain")
    success = CkMime::ckAddHeaderField(mime,"abc","123")
    CkMime::ckSetBody(mime,"This is a test")

    ; A digital certificate is required to create PKCS7 encrypted MIME.
    ; It can come from a variety of sources: .cer file, .pfx file, PEM files,
    ; an in-memory representation, or directly from a Windows
    ; registry-based certificate store.

    ; Load one cert from a .cer file:
    cert1.i = CkCert::ckCreate()
    If cert1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadFromFile(cert1,"myCert.cer")
    If success = 0
        Debug CkCert::ckLastErrorText(cert1)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert1)
        ProcedureReturn
    EndIf

    ; Load another cert ...
    cert2.i = CkCert::ckCreate()
    If cert2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadFromFile(cert2,"anotherCert.cer")
    If success = 0
        Debug CkCert::ckLastErrorText(cert2)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert1)
        CkCert::ckDispose(cert2)
        ProcedureReturn
    EndIf

    ; Tell the MIME object to use these certs for encrypting:
    ; (AddEncryptCert shouldn't fail if we already know it contains
    ; a valid certificate..)
    success = CkMime::ckAddEncryptCert(mime,cert1)
    success = CkMime::ckAddEncryptCert(mime,cert2)

    ; Encrypt the MIME:
    success = CkMime::ckEncryptN(mime)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert1)
        CkCert::ckDispose(cert2)
        ProcedureReturn
    EndIf

    ; Display the MIME:
    Debug CkMime::ckGetMime(mime)

    ; The resulting S/MIME looks something like this:

    ; abc: 123
    ; Content-Disposition: attachment; filename="smime.p7m"
    ; Content-Transfer-Encoding: base64
    ; Content-Type: application/x-pkcs7-mime;
    ;  name="smime.p7m"
    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rdFG2doe6qHiRtAxxPrd0GmQRcGvxyDdsojkoBlTMbfwaXOI3
    ; xepR/oh60x/3npUCsl2VpNPfo6oZoej6YZGEuz2MvuWvXVhvuqoTqXGwGDgYMxwTbPwklqZiJtsK
    ; r5Ep3NlWAQX8MGWL0H+NC22cqJuqUCL05yqyAv+JMCpZGAwOVb/wiVNjpAIwYAYJKoZIhvcNAQcB
    ; MBkGCCqGSIb3DQMCMA0CAToECA5NAJY12cU+gDgZFUc+nsQUUbiR1GH8iBVLI+XWlja9TXEe1Hzt
    ; NlsOylRVBDzjNxUtX/zC0YR7bs+2Cf4JbyuMOA==


    CkMime::ckDispose(mime)
    CkCert::ckDispose(cert1)
    CkCert::ckDispose(cert2)


    ProcedureReturn
EndProcedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.