Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.71+

Encrypt / Decrypt Secure Strings

See more Encryption Examples

Demonstrates how to use the EncryptSecureENC and DecryptSecureENC methods to encrypt/decrypt secure strings.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

-- Load the secure string with some text.
local secStr1 = chilkat.SecureString.new()
local success = secStr1:load_file("qa_data/txt/helloWorld.txt", "utf-8")
if not success then
    print("Failed to load helloWorld.txt")
    return
end

local crypt = chilkat.Crypt2.new()

crypt.crypt_algorithm = "aes"
crypt.cipher_mode = "cbc"
crypt.key_length = 128
crypt:set_encoded_key("000102030405060708090A0B0C0D0E0F", "hex")
crypt:set_encoded_iv("000102030405060708090A0B0C0D0E0F", "hex")
crypt.encoding_mode = "base64"

-- Return the base64 encoded encrypted contents of secStr1.
local encryptedStr = crypt:encrypt_secure_enc(secStr1)
print("Encrypted string: " .. encryptedStr)

-- Output:
-- Encrypted string: qiq+IFhcjTkEIkZyf31V/g==

-- Decrypt to secStr2:
local secStr2 = chilkat.SecureString.new()
crypt:decrypt_secure_enc(encryptedStr, secStr2)

-- Access the contents of secStr2
print("Decrypted string: " .. secStr2:access())

-- Output:
-- Decrypted string: Hello World!