PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkEmail.pb"
IncludeFile "CkCert.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the LastDecryptCert method, which copies the certificate used to decrypt
; this email into a Cert object. Call it after a successful decryption.
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkEmail::ckLoadEml(email,"qa_data/eml/encrypted.eml")
If success = 0
Debug CkEmail::ckLastErrorText(email)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
If CkEmail::ckReceivedEncrypted(email) = 1
If CkEmail::ckDecrypted(email) = 1
; Get the certificate that was used to decrypt the email.
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkEmail::ckLastDecryptCert(email,cert)
If success = 1
Debug "Decrypted with certificate (CN): " + CkCert::ckSubjectCN(cert)
EndIf
EndIf
EndIf
; Note: The path "qa_data/..." is a relative local filesystem path,
; relative to the current working directory of the running application.
CkEmail::ckDispose(email)
CkCert::ckDispose(cert)
ProcedureReturn
EndProcedure