Sample code for 30+ languages & platforms
PowerBuilder

Encrypt MIME for a Recipient Certificate

See more MIME Examples

Demonstrates encrypting the current MIME entity for a single recipient using the Mime.Encrypt method. The entity is replaced with CMS/PKCS #7 encrypted S/MIME that only the holder of the recipient's private key can read.

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. S/MIME encryption uses the recipient's public certificate to protect a message so that only the matching private key can recover it. The recipient certificate must be RSA-based. Only the recipient's public key is needed to encrypt—the private key is required to decrypt.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mime
oleobject loo_Cert

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.SetBodyFromPlainText("This message will be encrypted.")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  Load the recipient's certificate (public key only is sufficient for encrypting).
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

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

//  Encrypt for the recipient.  The entity is replaced with CMS/PKCS #7 encrypted S/MIME that only
//  the holder of the recipient's private key can decrypt.  The certificate must be RSA-based.
li_Success = loo_Mime.Encrypt(loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_Cert
    return
end if

li_Success = loo_Mime.SaveMime("qa_output/encrypted.eml")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_Cert
    return
end if

Write-Debug "Message encrypted."


destroy loo_Mime
destroy loo_Cert