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

Extract PKCS7 from MIME and Decrypt

See more MIME Examples

Extracts the base64-encoded PKCS7 body of a MIME message to a file, and then decrypts using Chilkat Crypt2.

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()

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

success = mime:save_body("c:/aaworkarea/smime.p7m")
if not success then
    print(mime.last_error_text)
    return
end

local crypt = chilkat.Crypt2.new()

-- The certificate and its private key are in a PEM file.  The 2nd argument of LoadPemFile is the
-- password of the private key.  (Pass an empty string if the private key is not encrypted.)
local pem = chilkat.Pem.new()
success = pem:load_pem_file("c:/aaworkarea/my.pem", "pemPassword")
if not success then
    print(pem.last_error_text)
    return
end

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

-- Indicate the public-key (PKCS7) encryption/decryption should be used:
crypt.crypt_algorithm = "pki"

local inPath = "c:/aaworkarea/smime.p7m"
local outPath = "c:/aaworkarea/decrypted.dat"

success = crypt:ck_decrypt_file(inPath, outPath)
if not success then
    print(crypt.last_error_text)
    return
end

print("Success.")