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

RSA Encrypt and OpenSSL Decrypt

See more OpenSSL Examples

Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local rsa = chilkat.Rsa.new()

local privKey = chilkat.PrivateKey.new()
local success = rsa:gen_key(2048, privKey)
success = privKey:save_pkcs8_pem_file("qa_output/privKey.pem")

local pubKey = chilkat.PublicKey.new()
privKey:to_public_key(pubKey)

rsa.encoding_mode = "base64"
local plainText = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890"
local bUsePrivateKey = false
rsa:use_public_key(pubKey)
local encryptedStr = rsa:encrypt_string_enc(plainText, bUsePrivateKey)

local bd = chilkat.BinData.new()
bd:append_encoded(encryptedStr, "base64")
success = bd:write_file("qa_output/enc.dat")

-- The OpenSSL command to decrypt is:
-- openssl pkeyutl -in enc.dat -inkey privKey.pem -keyform PEM -pkeyopt rsa_padding_mode:pkcs1 -decrypt

print("OK")