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

RSA Encrypt with Modulus and Exponent

See more RSA Examples

Demonstrates how to RSA encrypt with a given modulus and exponent.

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

-- Assuming you already have a base64 modulus and exponent,
-- wrap it in XML like this:
local modulus = "qMBRpdYrAy5aMmo31NErUizh5sbweguSmh4wlK6uJEIDl+kwTlROnE34KOFExeTbJSX0WygPi+vWl0yNq7buIMUKpytossAAWut5khO3CQJxTk7G2gnEPNUUXHiExGgNrLzcSLv8YIlfVALhoRWyC67KOL+a+3taNq3h+BHeWhM="
local exponent = "AQAB"

local xml = chilkat.Xml.new()
xml.tag = "RSAPublicKey"
xml:new_child2("Modulus", modulus)
xml:new_child2("Exponent", exponent)

local pubKey = chilkat.PublicKey.new()
local success = pubKey:load_from_string(xml:get_xml())
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

local usePrivateKey = false
local plainText = "message in a bottle"

rsa.encoding_mode = "base64"
local encryptedStrBase64 = rsa:encrypt_string_enc(plainText, usePrivateKey)
print(encryptedStrBase64)