Sample code for 30+ languages & platforms
Lua

Add a Recipient Certificate for S/MIME Encryption

See more MIME Examples

Demonstrates AddEncryptCert, which adds one recipient certificate to the list used by EncryptN. Call it once for each intended recipient, then call EncryptN to encrypt for all of them.

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. AddEncryptCert builds up the recipient list used by EncryptN. Only the public key of each certificate is required for encryption.

Chilkat Lua Downloads

Lua
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

-- AddEncryptCert adds one recipient certificate to the list used by EncryptN.  Call it once per
-- recipient.
local cert = chilkat.Cert.new()
success = cert:load_from_file("qa_data/recipient.cer")
if not success then
    print(cert.last_error_text)
    return
end

success = mime:add_encrypt_cert(cert)
if not success then
    print(mime.last_error_text)
    return
end

-- EncryptN then encrypts for all certificates added.
success = mime:encrypt_n()
if not success then
    print(mime.last_error_text)
    return
end

print("Encrypted for the added recipient(s).")