Cryptography

crypt.decrypt

Decrypts Base64 ciphertext with a Base64-decoded key and required Base64 IV using AES CBC or ECB.

Syntax

crypt.decrypt(data: string, key: string, iv: string, mode?: "CBC" | "ECB" | "AES-CBC" | "AES-ECB"): string

Arguments

NameTypeDescription
datastringBase64 ciphertext decoded before decryption.
keystringBase64 key decoded before it is passed to AES.
ivstringBase64 IV decoded before mode handling. This argument is required, including for ECB.
mode?"CBC" | "ECB" | "AES-CBC" | "AES-ECB"AES mode. The AES- prefix is removed when present. Defaults to AES-CBC.

Returns

NameTypeDescription
plaintextstringPlaintext bytes returned as a Lua string.

Description

Decrypts Base64 ciphertext with a Base64-decoded key and required Base64 IV using AES CBC or ECB.

Call it with 4 parameter(s): data, key, iv, mode. The argument table explains which values are required and which ones only refine the behavior.

It returns plaintext (string). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Decrypt ciphertext returned by crypt.encrypt using the returned IV.

local key = crypt.generatekey()
local ciphertext, iv = crypt.encrypt("hello world", key)

local plaintext = crypt.decrypt(ciphertext, key, iv)

print(plaintext)