Cryptography

crypt.encrypt

Encrypts a string with the provided key and returns the ciphertext together with the initialization vector used.

Syntax

crypt.encrypt(data: string, key: string, iv?: string, mode?: "CBC" | "ECB" | "CTR"): string | string | nil

Arguments

NameTypeDescription
datastringInput byte string or text processed by the function.
keystringCryptographic key used by the operation.
iv?stringInitialization vector used by the cipher. Provide it for decrypting existing data.
mode?"CBC" | "ECB" | "CTR"AES mode. Defaults to CBC.

Returns

NameTypeDescription
ciphertextstringEncrypted text returned by the cipher.
initializationVectorstring | nilBase64-encoded initialization vector for CBC and CTR; nil for ECB.

Description

Encrypts a string with the provided key and returns the ciphertext together with the initialization vector used.

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 ciphertext (string), initializationVector (string | nil). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Example call with placeholder values.

local result = crypt.encrypt("example", "example", "example", nil)
print(result)