Sample code for 30+ languages & platforms
Lua

Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

See more OpenSSL Examples

How to create a PKCS12 (.p12 or .pfx) from a certificate file and private key file: Demonstrates how to duplicate this OpenSSL command:
Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

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.

local pkey = chilkat.PrivateKey.new()

-- Load the private key from the file.
local success = pkey:load_any_format_file("certFile.key", "")
if not success then
    print(pkey.last_error_text)
    return
end

local cert = chilkat.Cert.new()
-- The LoadFromFile method auto-recognizes the format...
success = cert:load_from_file("certfile.cer")
if not success then
    print(cert.last_error_text)
    return
end

-- We'll need a cert chain object to create the PKCS12, so get it
-- from the cert.
local certChain = ERROR: Method not in Chilkat for Lua: Cert.GetCertChain
cert:ERROR()
if not cert.last_method_success then
    print(cert.last_error_text)
    return
end

-- Create the PFX object, add the cert and private key, and write to a .pfx file.
local pfx = chilkat.Pfx.new()

-- The cert(s) are automatically added in the call to AddPrivateKey
success = pfx:add_private_key(pkey, certChain)
if not success then
    print(pfx.last_error_text)
    return
end

-- Write the .pfx to a file.
local password = "myPassword"
success = pfx:to_file(password, "certfile.pfx")
if not success then
    print(pfx.last_error_text)
    return
end

print("Success.")