Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.49+

Add Private Key and Certificate to a PEM

See more PEM Examples

Demonstrates how to add certificates and private keys to a PEM.

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.

-- The Chilkat PEM class requires the bundle to be unlocked, as shown above.
local pem = chilkat.Pem.new()

-- Add the private key found in alice.key to this PEM.
--
local privKey = chilkat.PrivateKey.new()

local success = privKey:load_any_format_file("qa_data/alice.key", "")
if not success then
    print(privKey.last_error_text)
    return
end

-- Add it to the PEM:
success = pem:add_private_key(privKey)

-- Add the certificate found in alice.crt to this PEM.
--
local cert = chilkat.Cert.new()

success = cert:load_from_file("qa_data/alice.crt")
if not success then
    print(cert.last_error_text)
    return
end

-- Add it to the PEM:
local includeCertChain = false
success = pem:add_cert(cert, includeCertChain)

-- Write the PEM containing the private key and certificate.
-- The private key will be output in PKCS8 encrypted form.
-- Certificates are never encrypted.

-- This is the password that will be required to open and access the private key
-- from the PEM we're about to write..
local password = "secret"
local extendedAttrs = false
local noKeys = false
local noCerts = false
local noCaCerts = false
local encryptAlg = "aes128"

local pemStr = pem:to_pem_ex(extendedAttrs, noKeys, noCerts, noCaCerts, encryptAlg, password)
print(pemStr)