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

Convert PKCS12 / PFX to Java Keystore (JKS)

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and saves it to a Java keystore (JKS) file.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local pfx = chilkat.Pfx.new()

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

local jksPassword = "myJksPassword"
local alias = "firstPrivateKeyAlias"

local jks = chilkat.JavaKeyStore.new()

-- Convert to a Java keystore object.
-- The jksPassword is the password to be used for the JKS private key entries.
-- It may be the same as the PFX password, but can also be different if desired.
success = pfx:to_jks_obj(alias, jksPassword, jks)
if not success then
    print(pfx.last_error_text)
    return
end

-- Save the Java keystore to a file.
success = jks:to_file(jksPassword, "/myKeystores/my.jks")
if not success then
    print(jks.last_error_text)

    return
end

print("Successfully converted PFX to JKS.")