Sample code for 30+ languages & platforms
DataFlex

Encrypt MIME for Multiple Recipients

See more MIME Examples

Demonstrates encrypting the current MIME entity for several recipients at once. A certificate is added for each recipient with AddEncryptCert, and EncryptN then produces a single CMS/PKCS #7 message that each recipient can independently decrypt.

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. When a message targets more than one recipient, each recipient's certificate is added to the internal list and EncryptN wraps the message key separately for each one. Any single recipient can then decrypt with their own private key.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMime
    Variant vCert1
    Handle hoCert1
    Variant vCert2
    Handle hoCert2
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get ComSetBodyFromPlainText Of hoMime "This message will be encrypted." To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Add a certificate for each intended recipient.  Every recipient receives an independently
    //  wrapped copy of the message key.
    Get Create (RefClass(cComChilkatCert)) To hoCert1
    If (Not(IsComObjectCreated(hoCert1))) Begin
        Send CreateComObject of hoCert1
    End
    Get ComLoadFromFile Of hoCert1 "qa_data/recipient1.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert1 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoCert1 to vCert1
    Get ComAddEncryptCert Of hoMime vCert1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCert)) To hoCert2
    If (Not(IsComObjectCreated(hoCert2))) Begin
        Send CreateComObject of hoCert2
    End
    Get ComLoadFromFile Of hoCert2 "qa_data/recipient2.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoCert2 to vCert2
    Get ComAddEncryptCert Of hoMime vCert2 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Encrypt for every added recipient at once.
    Get ComEncryptN Of hoMime To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSaveMime Of hoMime "qa_output/encrypted_multi.eml" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Message encrypted for multiple recipients."


End_Procedure