Lua
Lua
Get RSA Key Modulus from .cer or .key
See more Certificates Examples
Demonstrates how to get the RSA key modulus from either the certificate (.cer) or RSA key (.key). OpenSSL commands to do the same would be:openssl x509 -inform DER -in "test.cer" -modulus -nooutor
openssl pkcs8 -inform DER -inβ "test.key"β -outform PEM -passin pass:"12345β678aβ" | openssl rsa -inform PEM -modulus -noout
Chilkat Lua Downloads
local chilkat = require("chilkat")
local privKey = chilkat.PrivateKey.new()
local password = "12345678a"
local success = privKey:load_pkcs8_encrypted_file("qa_data/certs/test_12345678a.key", password)
if not success then
print(privKey.last_error_text)
return
end
local xml = chilkat.Xml.new()
xml:load_xml(privKey:get_xml())
-- The XML contains the parts of the key in base64.
print("Private Key XML:")
print(xml:get_xml())
-- We can get the base64 modulus like this:
local modulus = xml:get_child_content("Modulus")
print("base64 modulus = " .. modulus)
-- To convert to hex:
local binDat = chilkat.BinData.new()
binDat:append_encoded(modulus, "base64")
local hexModulus = binDat:get_encoded("hex")
print("hex modulus = " .. hexModulus)
-- Now get the modulus from the cert:
local cert = chilkat.Cert.new()
success = cert:load_from_file("qa_data/certs/test_12345678a.cer")
if not success then
print(cert.last_error_text)
return
end
-- The cert contains the public key, which is composed of the
-- modulus + exponent (for RSA keys).
local pubKey = chilkat.PublicKey.new()
cert:get_public_key(pubKey)
xml:load_xml(pubKey:get_xml())
print("Public Key XML:")
print(xml:get_xml())
-- Proceed in the same way as before....
modulus = xml:get_child_content("Modulus")
print("base64 modulus = " .. modulus)
-- To convert to hex:
binDat:clear()
binDat:append_encoded(modulus, "base64")
hexModulus = binDat:get_encoded("hex")
print("hex modulus = " .. hexModulus)