Sample code for 30+ languages & platforms
Lua

Decrypt2

See more MIME Examples

Demonstrates how to decrypt MIME using a certificate and private key where the certificate and its corresponding private key are stored in separate files -- a .cer for the certificate, and a .pem for the private key.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local mime = chilkat.Mime.new()

-- Load the certificate from a .cer file.
local cert = chilkat.Cert.new()
local success = cert:load_from_file("aaworkarea/myCert.cer")
if not success then
    print(cert.last_error_text)
    return
end

-- Load the private key from an encrypted PEM file.
-- (A private key can be loaded from other file formats also..)
local privKey = chilkat.PrivateKey.new()
success = privKey:load_encrypted_pem_file("aaworkarea/myPrivateKey.pem", "myPassword")
if not success then
    print(privKey.last_error_text)
    return
end

success = mime:load_mime_file("aaworkarea/encryptedMime.txt")
if not success then
    print(mime.last_error_text)
    return
end

-- Decrypt using the cert and associated private key
success = mime:decrypt2(cert, privKey)
if not success then
    print(mime.last_error_text)
    return
end

-- Save the S/MIME to a file.
success = mime:save_mime("aaworkarea/decryptedMime.txt")
if not success then
    print(mime.last_error_text)
    return
end

print("success!")