Sample code for 30+ languages & platforms
DataFlex

Set the Decryption Certificate and Private Key

See more Email Object Examples

Demonstrates the Chilkat Email.SetDecryptCert2 method, which explicitly provides a certificate and its corresponding private key as separate objects for decrypting a received encrypted email. Set them before loading the encrypted message. This example loads a .cer certificate and a PEM private key, sets both, then loads an encrypted email.

Background: Sometimes the certificate and its private key are stored separately — a public .cer file plus a PEM (or other format) key file — rather than combined in a PFX. SetDecryptCert2 accepts the two objects individually, which is the natural fit for that arrangement. It is the two-object counterpart to SetDecryptCert, which takes a single certificate that already carries its private key.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Variant vCert
    Handle hoCert
    Variant vPrivKey
    Handle hoPrivKey
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates the SetDecryptCert2 method, which explicitly provides a certificate and its
    //  corresponding private key (as separate objects) for decrypting a received encrypted
    //  email.  Set them before loading the encrypted email.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End

    //  Load the certificate (public) and the matching private key from separate files.
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/certs/recipient.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get ComLoadPemFile Of hoPrivKey "qa_data/certs/recipient_privkey.pem" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Provide the certificate and private key to use for decryption.
    Get pvComObject of hoCert to vCert
    Get pvComObject of hoPrivKey to vPrivKey
    Get ComSetDecryptCert2 Of hoEmail vCert vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Load the encrypted email; Chilkat decrypts it using the certificate and key.
    Get ComLoadEml Of hoEmail "qa_data/eml/encrypted.eml" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComDecrypted Of hoEmail To bTemp1
    Showln "Decrypted = " bTemp1

    //  Note: Paths such as "qa_data/..." are relative local filesystem paths,
    //  relative to the current working directory of the running application.


End_Procedure