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

Export Digital Certificate's Public Key

See more Certificates Examples

The ExportPublicKey method can be called to get a certificate's public key. It can then be saved to any of a number of formats: (1) OpenSSL DER, (2) OpenSSL PEM, (3) RSA DER, (4) XML.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local cert = chilkat.Cert.new()

-- LoadFromFile will load virtually any certificate format file.
-- It will auto-recognize the format and load appropiately.
local success = cert:load_from_file("/Users/chilkat/testData/cer/chilkat.cer")
if not success then
    print(cert.last_error_text)
    return
end

-- Get the public key:
local pubkey = chilkat.PublicKey.new()
cert:get_public_key(pubkey)

-- Save to various formats:
success = pubkey:save_der_file(false, "/Users/chilkat/testData/pubkeys/chilkat_pkcs8.der")
if not success then
    print(pubkey.last_error_text)
    return
end

success = pubkey:save_pem_file(false, "/Users/chilkat/testData/pubkeys/chilkat.pem")
if not success then
    print(pubkey.last_error_text)
    return
end

success = pubkey:save_der_file(true, "/Users/chilkat/testData/pubkeys/chilkat_pkcs1.der")
if not success then
    print(pubkey.last_error_text)
    return
end

success = pubkey:save_xml_file("/Users/chilkat/testData/pubkeys/chilkat.xml")
if not success then
    print(pubkey.last_error_text)
    return
end

print("Public key exported to all file formats.")