Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Encrypt with RSA-OAEP and AES-128-CBC

See more Email Object Examples

Use this profile when the receiver requires AES-128-CBC content encryption and RSA-OAEP with SHA-256. The SHA-256 settings apply to OAEP and its mask generation function. Use the matching decryption example to inspect the result.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Email
oleobject loo_Cert
oleobject loo_Mail
oleobject loo_Rendered

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

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("message.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

//  Use the recipient's public encryption certificate. Its private key stays with the recipient.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadFromFile("recipient.cer")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    return
end if

li_Success = loo_Email.SetEncryptCert(loo_Cert)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    return
end if

loo_Email.Pkcs7CryptAlg = "aes"
loo_Email.Pkcs7KeyLength = 128
loo_Email.OaepPadding = 1
loo_Email.OaepHash = "sha256"
loo_Email.OaepMgfHash = "sha256"
loo_Email.SendEncrypted = 1
loo_Mail = create oleobject
li_rc = loo_Mail.ConnectToNewObject("Chilkat.MailMan")

loo_Rendered = create oleobject
li_rc = loo_Rendered.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Mail.RenderToMimeBd(loo_Email,loo_Rendered)
if li_Success <> 1 then
    Write-Debug loo_Mail.LastErrorText
    destroy loo_Email
    destroy loo_Cert
    destroy loo_Mail
    destroy loo_Rendered
    return
end if

li_Success = loo_Rendered.WriteFile("oaep-encrypted.eml")
if li_Success <> 1 then
    Write-Debug "Failed: rendered.WriteFile."
    destroy loo_Email
    destroy loo_Cert
    destroy loo_Mail
    destroy loo_Rendered
    return
end if

Write-Debug "Saved oaep-encrypted.eml."


destroy loo_Email
destroy loo_Cert
destroy loo_Mail
destroy loo_Rendered