Sample code for 30+ languages & platforms
Lua

Encrypt / Decrypt a File and Verify it has not Changed

See more Encryption Examples

Demonstrates how to encrypt and decrypt a file, and verify it has not changed.

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 crypt = chilkat.Crypt2.new()

crypt.crypt_algorithm = "aes"
crypt.cipher_mode = "cbc"
crypt.key_length = 128
crypt.padding_scheme = 0

local ivHex = "000102030405060708090A0B0C0D0E0F"
crypt:set_encoded_iv(ivHex, "hex")

local keyHex = "00010203040506071011121314151617"
crypt:set_encoded_key(keyHex, "hex")

local dataFile = "qa_data/zips/HBIQ040615300005.ZIP"
local outFile = "qa_output/HBIQ040615300005.enc"
local outFile2 = "qa_output/HBIQ040615300005.ZIP"

local success = crypt:ck_encrypt_file(dataFile, outFile)
success = crypt:ck_decrypt_file(outFile, outFile2)

local fac = chilkat.FileAccess.new()
local bEqual = fac:file_contents_equal(dataFile, outFile2)
if not bEqual then
    print("Decrypted file not equal to the original.")
else
    print("Success.")
end