Sample code for 30+ languages & platforms
PureBasic

Add a Recipient Certificate for S/MIME Encryption

See more MIME Examples

Demonstrates AddEncryptCert, which adds one recipient certificate to the list used by EncryptN. Call it once for each intended recipient, then call EncryptN to encrypt for all of them.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. AddEncryptCert builds up the recipient list used by EncryptN. Only the public key of each certificate is required for encryption.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

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

    success = CkMime::ckSetBodyFromPlainText(mime,"This message will be encrypted.")
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ;  AddEncryptCert adds one recipient certificate to the list used by EncryptN.  Call it once per
    ;  recipient.
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadFromFile(cert,"qa_data/recipient.cer")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    success = CkMime::ckAddEncryptCert(mime,cert)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ;  EncryptN then encrypts for all certificates added.
    success = CkMime::ckEncryptN(mime)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    Debug "Encrypted for the added recipient(s)."


    CkMime::ckDispose(mime)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure