crypt.encrypt

Encrypts data with AES-CBC.

Syntax

Signature
crypt.encrypt(Data: string, Key: string, Iv: string?, Mode: string?) -> (string, string)

Aliases

This function has no aliases.

Parameters

ParameterTypeDescription
DatastringPlaintext bytes, up to 16 MiB.
KeystringBase64 AES key.
Ivstring?Base64 16-byte IV. Omit to generate one.
Modestring?Only the exact string CBC is supported. Defaults to CBC.

Returns

TypeDescription
stringBase64 ciphertext.
stringBase64 IV used for encryption.

Notes

Key must be Base64 that decodes to 16, 24, or 32 bytes. Iv must decode to 16 bytes. If Iv is omitted, a random IV is generated. Returns Base64 ciphertext and the Base64 IV. CBC encryption does not authenticate the message.

Example

Luau
local Key = crypt.generatekey()
local Ciphertext, Iv = crypt.encrypt("A message", Key)

print("Ciphertext:", Ciphertext)
local Plaintext = crypt.decrypt(Ciphertext, Key, Iv)
assert(Plaintext == "A message")
-- Keep the key and IV if the ciphertext must be decrypted later.