crypt.decrypt Synchronous

Decrypt AES ciphertext using its key, initialization vector, and cipher mode. Use it to recover data written by the matching encryption call. The key, mode, and IV must agree with encryption; catch errors when reading damaged or untrusted ciphertext.

Syntax
Luau
crypt.decrypt(data: string, key: string, iv: string, mode: string?) -> string

Parameters

Function parameters
ParameterTypeDescription
datastringBase64 ciphertext. The decoded ciphertext is limited to 16 MiB.
keystringBase64-encoded 32-byte key used for encryption.
ivstringBase64 initialization vector returned by encryption.
modestring?AES mode: CBC, ECB, CTR, CFB, OFB, or GCM. Defaults to CBC; names are case insensitive.

Returns

string

The plaintext bytes.

Usage notes

Keys decode to exactly 32 bytes. CBC, CTR, CFB, and OFB use a 16-byte IV; a 32-byte IV is truncated for compatibility. GCM generates a 12-byte IV and accepts supplied IVs of at least 12 bytes. ECB ignores the IV.

Use the same mode for encryption and decryption. Ciphertext and IV are Base64 strings; plaintext is raw bytes.

Example

Example
Luau
local key = crypt.generatekey()
local encrypted, iv = crypt.encrypt("private example note", key, nil, "GCM")
local restored = crypt.decrypt(encrypted, key, iv, "GCM")
print(restored) -- private example note
Kawaii documentation