Sample code for 30+ languages & platforms
PowerBuilder

Get the Certificate Used for Decryption

See more MIME Examples

Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.

After a successful decryption, LastDecryptCert loads the certificate(s) actually used into a Cert object, indexed from zero up to NumDecryptCerts minus one.

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. Following decryption or security unwrapping, Chilkat records which certificate(s) were used. LastDecryptCert retrieves them so the application can report or verify the identity involved.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mime
string ls_PfxPassword
integer n
oleobject loo_Cert
integer i

li_Success = 0

loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")
if li_rc < 0 then
    destroy loo_Mime
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Mime.LoadMimeFile("qa_data/encrypted.eml")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  Configure a decryption source and decrypt.
ls_PfxPassword = "myPfxPassword"
li_Success = loo_Mime.AddPfxSourceFile("qa_data/recipient.pfx",ls_PfxPassword)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

li_Success = loo_Mime.Decrypt()
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  After a successful decryption, load the certificate(s) that were used.  The 1st argument is the
//  zero-based index and the 2nd is a Cert that receives the certificate.
n = loo_Mime.NumDecryptCerts
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

for i = 0 to n - 1
    li_Success = loo_Mime.LastDecryptCert(i,loo_Cert)
    if li_Success = 0 then
        Write-Debug loo_Mime.LastErrorText
        destroy loo_Mime
        destroy loo_Cert
        return
    end if

    Write-Debug "Decrypted with: " + loo_Cert.SubjectCN
next


destroy loo_Mime
destroy loo_Cert