Lua Requires Chilkat v11.0.0+
Lua
Generate an RSA Key and Get as Base64 DER
See more RSA Examples
Demonstrates how to generate a 2048-bit RSA key and return the public and private parts as unencrypted Base64 encoded DER.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local rsa = chilkat.Rsa.new()
-- Generate a 2048-bit key.
local privKey = chilkat.PrivateKey.new()
local success = rsa:gen_key(2048, privKey)
if not success then
print(rsa.last_error_text)
return
end
-- Get the public part of the key.
local pubKey = chilkat.PublicKey.new()
privKey:to_public_key(pubKey)
-- There are two possible formats for representing the RSA public key
-- in ASN.1 (DER). The possible formats are PKCS1 and PKCS8.
-- We can get either by setting bChoosePkcs1 to true or false.
local bChoosePkcs1 = true
local pubKeyBase64Der = pubKey:get_encoded(bChoosePkcs1, "base64")
print("Public Key Base64 DER:")
print(pubKeyBase64Der)
-- Get the private key as Base64 DER:
-- We can get PKCS1 or PKCS8, but with different methods:
local privKeyPkcs1 = privKey:get_pkcs1_enc("base64")
print("Private Key PKCS1 Base64 DER:")
print(privKeyPkcs1)
local privKeyPkcs8 = privKey:get_pkcs8_enc("base64")
print("Private Key PKCS8 Base64 DER:")
print(privKeyPkcs8)