Sample code for 30+ languages & platforms
Lua

Load Identities from PEM Text

See more PFX/P12 Examples

Demonstrates Pfx.LoadPem, which loads one or more certificate/private-key identities from PEM-formatted text and builds the contents of the Pfx object.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The password decrypts any encrypted private keys in the PEM. This builds a PFX in memory from PEM material, which can then be serialized to PKCS#12.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local pfx = chilkat.Pfx.new()

-- The file path is relative to the application's current working directory.  An absolute path
-- may also be used.  Supply the path appropriate to your own environment.
-- Load the PEM text (certificates and private keys) into a string.
local sbPem = chilkat.StringBuilder.new()
local bLoaded = sbPem:load_file("qa_data/identity.pem", "utf-8")
if not bLoaded then
    print("Failed to load the PEM file.")
    return
end

local pemText = sbPem:get_as_string()
if not sbPem.last_method_success then
    print(sbPem.last_error_text)
    return
end

-- The password decrypts any encrypted private keys in the PEM (use an empty string if none are
-- encrypted).  A password should come from a secure source rather than being hard-coded.
local password = "myPemPassword"
local success = pfx:load_pem(pemText, password)
if not success then
    print(pfx.last_error_text)
    return
end

print("Loaded " .. pfx.num_private_keys .. " private key(s).")