Lua
Lua
Decrypt S/MIME with an Explicit Certificate and Private Key
See more MIME Examples
Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.
Decrypt2 accepts the certificate and its private key directly as arguments.
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 the certificate and private key are already loaded as separate objects, Decrypt2 uses them directly rather than searching an internal collection of credential sources.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local mime = chilkat.Mime.new()
local success = mime:load_mime_file("qa_data/encrypted.eml")
if not success then
print(mime.last_error_text)
return
end
-- Decrypt using an explicitly supplied certificate and private key. The 1st argument is the
-- certificate and the 2nd is its private key.
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
local privKey = chilkat.PrivateKey.new()
success = privKey:load_pem_file("qa_data/recipient_privkey.pem")
if not success then
print(privKey.last_error_text)
return
end
success = mime:decrypt2(cert, privKey)
if not success then
print(mime.last_error_text)
return
end
local body = mime:get_body_decoded()
if not mime.last_method_success then
print(mime.last_error_text)
return
end
print(body)