Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.8.0+

PKCS7 Decrypt MIME

See more MIME Examples

Loads a PKCS7 encrypted MIME file and decrypts. The cert and private key used for decryption is loaded from a PEM file.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local mime = chilkat.Mime.new()

-- Load the MIME
local success = mime:load_mime_file("encryptedMime.txt")
if not success then
    print(mime.last_error_text)
    return
end

-- The AddPemSource method may be called one or more times
-- (one per Pem object) to add sources from which the MIME component will
-- search for certificates and private keys when decrypting.
-- Here the certificate and its private key are loaded from a PEM file.
-- The 2nd argument of LoadPemFile is the password of the private key.
local pemPassword = "myPassword"
local pem = chilkat.Pem.new()
success = pem:load_pem_file("myCertAndPrivateKey.pem", pemPassword)
if not success then
    print(pem.last_error_text)
    return
end

success = mime:add_pem_source(pem)
if not success then
    print(mime.last_error_text)
    return
end

-- Decrypt...
success = mime:decrypt()
if not success then
    print(mime.last_error_text)
    return
end

-- Display the decrypted MIME:
print(mime:get_mime())