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

Load PKCS12 / PFX and Access Contents

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local pfx = chilkat.Pfx.new()

-- Load the PKCS12 from a file
local success = pfx:load_pfx_file("/someDir/my.p12", "pfxFilePassword")
if not success then
    print(pfx.last_error_text)
    return
end

local numPrivateKeys = pfx.num_private_keys

local privKey = chilkat.PrivateKey.new()

print("Private Keys:")

local i = 0
while i < numPrivateKeys do
    pfx:private_key_at(i, privKey)

    -- Do something with the private key ...

    i = i + 1
end

local cert = chilkat.Cert.new()

local numCerts = pfx.num_certs

print("Certs:")
i = 0
while i < numCerts do
    pfx:cert_at(i, cert)
    print(cert.subject_dn)

    -- If the certificate has a private key (one of the private keys within the PFX)
    -- then it can also be obtained via the certificate object:
    if cert:has_private_key() then
        print("Has private key!")

        success = cert:get_private_key(privKey)
        -- ...
    end

    i = i + 1
end