Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_Cert

li_Success = 0

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

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_Success = loo_Email.LoadEml("qa_data/eml/encrypted.eml")
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

if loo_Email.ReceivedEncrypted = 1 then
    if loo_Email.Decrypted = 1 then
        //  Get the certificate that was used to decrypt the email.
        loo_Cert = create oleobject
        li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

        li_Success = loo_Email.LastDecryptCert(loo_Cert)
        if li_Success = 1 then
            Write-Debug "Decrypted with certificate (CN): " + loo_Cert.SubjectCN
        end if

    end if

end if

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


destroy loo_Email
destroy loo_Cert