Sample code for 30+ languages & platforms
PowerBuilder

Encrypt MIME for Multiple Recipients

See more MIME Examples

Demonstrates encrypting the current MIME entity for several recipients at once. A certificate is added for each recipient with AddEncryptCert, and EncryptN then produces a single CMS/PKCS #7 message that each recipient can independently decrypt.

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. When a message targets more than one recipient, each recipient's certificate is added to the internal list and EncryptN wraps the message key separately for each one. Any single recipient can then decrypt with their own private key.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mime
oleobject loo_Cert1
oleobject loo_Cert2

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

//  Add a certificate for each intended recipient.  Every recipient receives an independently
//  wrapped copy of the message key.
loo_Cert1 = create oleobject
li_rc = loo_Cert1.ConnectToNewObject("Chilkat.Cert")

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

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

loo_Cert2 = create oleobject
li_rc = loo_Cert2.ConnectToNewObject("Chilkat.Cert")

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

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

//  Encrypt for every added recipient at once.
li_Success = loo_Mime.EncryptN()
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_Cert1
    destroy loo_Cert2
    return
end if

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

Write-Debug "Message encrypted for multiple recipients."


destroy loo_Mime
destroy loo_Cert1
destroy loo_Cert2