Lua
Lua
Get Public Key from CSR
See more CSR Examples
Demonstrates how to get the public key from a CSR.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local pem = chilkat.Pem.new()
-- No password is required. Pass an empty password string..
local noPassword = ""
local success = pem:load_pem_file("qa_data/csr/csr2.pem", noPassword)
if not success then
print(pem.last_error_text)
return
end
local strBase64 = pem:get_encoded_item("csr", "", "base64", 0)
local asn = chilkat.Asn.new()
success = asn:load_encoded(strBase64, "base64")
if not success then
print(asn.last_error_text)
return
end
-- Convert the ASN.1 to XML.
local xml = chilkat.Xml.new()
success = xml:load_xml(asn:asn_to_xml())
print(xml:get_xml())
print("----")
local strModulusHex = xml:get_child_content("bits")
print("strModulusHex = " .. strModulusHex)
print("----")
-- We need the modulus as base64.
local bd = chilkat.BinData.new()
bd:append_encoded(strModulusHex, "hex")
local modulus64 = bd:get_encoded("base64")
print("modulus64 = " .. modulus64)
print("----")
-- Build the XML for the public key.
local xmlPubKey = chilkat.Xml.new()
xmlPubKey.tag = "RSAPublicKey"
xmlPubKey:update_child_content("Modulus", modulus64)
-- The RSA exponent will always be decimal 65537 (base64 = AQAB)
xmlPubKey:update_child_content("Exponent", "AQAB")
print("RSA public key as XML:")
print(xmlPubKey:get_xml())
print("----")
-- Load the XML into a Chilkat public key object.
local pubkey = chilkat.PublicKey.new()
success = pubkey:load_from_string(xmlPubKey:get_xml())
if not success then
print(pubkey.last_error_text)
return
end
-- Show the public key as PEM.
local preferPkcs1 = true
print(pubkey:get_pem(preferPkcs1))