Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_Cert
oleobject loo_PrivKey

li_Success = 0

//  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.

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

//  Load the certificate (public) and the matching private key from separate files.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadFromFile("qa_data/certs/recipient.cer")
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    return
end if

loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_PrivKey.LoadPemFile("qa_data/certs/recipient_privkey.pem")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    destroy loo_PrivKey
    return
end if

//  Provide the certificate and private key to use for decryption.
li_Success = loo_Email.SetDecryptCert2(loo_Cert,loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    destroy loo_PrivKey
    return
end if

//  Load the encrypted email; Chilkat decrypts it using the certificate and key.
li_Success = loo_Email.LoadEml("qa_data/eml/encrypted.eml")
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    destroy loo_PrivKey
    return
end if

Write-Debug "Decrypted = " + string(loo_Email.Decrypted)

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


destroy loo_Email
destroy loo_Cert
destroy loo_PrivKey