Skip to content
Encoding & cryptographyReference

crypt.encrypt

Encrypt local data using AES-256-CBC.

Details#

Encrypt local data using AES-256-CBC. Two Base64 strings: ciphertext and the actual 16-byte IV. Invalid key, IV, mode or encryption failure raises an error.

Signature#

Luau
crypt.encrypt(data: string, key: string, iv: string?, mode: string?): (string, string)

Parameters#

ParameterTypeDescription
datastringRaw plaintext bytes.
keystringBase64 encoding of exactly 32 bytes.
ivoptionalstringBase64 IV containing at least 16 decoded bytes; omitted or nil generates a random IV. Only the first 16 bytes are used.
modeoptionalstringOnly CBC is accepted, case-insensitively; default CBC.

Returns#

Two Base64 strings: ciphertext and the actual 16-byte IV. Invalid key, IV, mode or encryption failure raises an error.

Examples#

Local example

Luau
local key = crypt.generatekey()
local ciphertext, iv = crypt.encrypt("Local note", key)
print(crypt.decrypt(ciphertext, key, iv)) -- Local note

Notes#

  • Keep your key private and save the returned IV for decryption. Encryption alone does not authenticate the data.