Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
local mime = chilkat.Mime.new()
local success = mime:set_body_from_plain_text("This message will be encrypted.")
if not success then
print(mime.last_error_text)
return
end
-- Add a certificate for each intended recipient. Every recipient receives an independently
-- wrapped copy of the message key.
local cert1 = chilkat.Cert.new()
success = cert1:load_from_file("qa_data/recipient1.cer")
if not success then
print(cert1.last_error_text)
return
end
success = mime:add_encrypt_cert(cert1)
if not success then
print(mime.last_error_text)
return
end
local cert2 = chilkat.Cert.new()
success = cert2:load_from_file("qa_data/recipient2.cer")
if not success then
print(cert2.last_error_text)
return
end
success = mime:add_encrypt_cert(cert2)
if not success then
print(mime.last_error_text)
return
end
-- Encrypt for every added recipient at once.
success = mime:encrypt_n()
if not success then
print(mime.last_error_text)
return
end
success = mime:save_mime("qa_output/encrypted_multi.eml")
if not success then
print(mime.last_error_text)
return
end
print("Message encrypted for multiple recipients.")