Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

$oMime = ObjCreate("Chilkat.Mime")
$bSuccess = $oMime.SetBodyFromPlainText("This message will be encrypted.")
If ($bSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

;  Add a certificate for each intended recipient.  Every recipient receives an independently
;  wrapped copy of the message key.
$oCert1 = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert1.LoadFromFile("qa_data/recipient1.cer")
If ($bSuccess = False) Then
    ConsoleWrite($oCert1.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oMime.AddEncryptCert($oCert1)
If ($bSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

$oCert2 = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert2.LoadFromFile("qa_data/recipient2.cer")
If ($bSuccess = False) Then
    ConsoleWrite($oCert2.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oMime.AddEncryptCert($oCert2)
If ($bSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

;  Encrypt for every added recipient at once.
$bSuccess = $oMime.EncryptN()
If ($bSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oMime.SaveMime("qa_output/encrypted_multi.eml")
If ($bSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Message encrypted for multiple recipients." & @CRLF)