Sample code for 30+ languages & platforms
DataFlex

Get the Certificate Used to Decrypt an Email

See more Email Object Examples

Demonstrates the Chilkat Email.LastDecryptCert method, which copies the certificate used to decrypt this email into a Cert object. It should be called after a successful decryption. This example loads an encrypted email and, if it was decrypted, reads the decrypting certificate's common name.

Background: When a message could be decrypted with one of several available private keys, it is often useful to know which certificate actually did the job — for auditing, for confirming the message was encrypted for the expected identity, or for logging. LastDecryptCert hands you that certificate as an object so you can inspect its subject, issuer, validity dates, and more.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

    //  Demonstrates the LastDecryptCert method, which copies the certificate used to decrypt
    //  this email into a Cert object.  Call it after a successful decryption.

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

    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 ComReceivedEncrypted Of hoEmail To bTemp1
    If (bTemp1 = True) Begin
        Get ComDecrypted Of hoEmail To bTemp1
        If (bTemp1 = True) Begin
            //  Get the certificate that was used to decrypt the email.
            Get Create (RefClass(cComChilkatCert)) To hoCert
            If (Not(IsComObjectCreated(hoCert))) Begin
                Send CreateComObject of hoCert
            End
            Get pvComObject of hoCert to vCert
            Get ComLastDecryptCert Of hoEmail vCert To iSuccess
            If (iSuccess = True) Begin
                Get ComSubjectCN Of hoCert To sTemp1
                Showln "Decrypted with certificate (CN): " sTemp1
            End

        End

    End

    //  Note: The path "qa_data/..." is a relative local filesystem path,
    //  relative to the current working directory of the running application.


End_Procedure