Lua Requires Chilkat v11.8.0+
Lua
Decrypt a .p7m File (using a Certificate and Private Key from a PEM File)
See more Encryption Examples
_LANGUAGE_ sample code showing how to decrypt a .p7m using a certificate w/ private key from a PEM file.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local crypt = chilkat.Crypt2.new()
-- Use a digital certificate and private key from 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 pemPath = "myCertAndKey.pem"
local pemPassword = "test123"
local pem = chilkat.Pem.new()
local success = pem:load_pem_file(pemPath, pemPassword)
if not success then
print(pem.last_error_text)
return
end
-- Give the certificate and private key to the Crypt2 object.
success = crypt:add_pem_source(pem)
if not success then
print(crypt.last_error_text)
return
end
-- Set the outPath to whatever is appropriate.
-- If you are decrypting a PDF, your output might be "out.pdf"...
local inPath = "smime.p7m"
local outPath = "original.txt"
-- Indicate that we're doing public key decryption (i.e. PKCS7)
crypt.crypt_algorithm = "pki"
success = crypt:ck_decrypt_file(inPath, outPath)
if not success then
print(crypt.last_error_text)
return
end
print("Success.")