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

Load PEM Public/Private Key into RSA Object

See more RSA Examples

Demonstrates how to load a PEM key into the Chilkat RSA object.

Chilkat Lua Downloads

Lua
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()

-- First demonstrate importing a PEM public key:
local publicKeyPem = "PEM public-key data goes here"
local pubkey = chilkat.PublicKey.new()

local success = pubkey:load_from_string(publicKeyPem)
if not success then
    print(pubkey.last_error_text)
    return
end

success = rsa:use_public_key(pubkey)
if not success then
    print(rsa.last_error_text)
    return
end

-- Demonstrate importing a PEM private key:
local privateKeyPem = "PEM private-key data goes here"
local privkey = chilkat.PrivateKey.new()

-- If the private key PEM is protected with a password, then
-- call LoadEncryptedPem.  Otherwise call LoadPem.
success = privkey:load_pem(privateKeyPem)
if not success then
    print(privkey.last_error_text)
    return
end

success = rsa:use_private_key(privkey)
if not success then
    print(rsa.last_error_text)
    return
end

print("OK!")