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
| Parameter | Type | Description |
|---|---|---|
Data | string | Plaintext bytes, up to 16 MiB. |
Key | string | Base64 AES key. |
Iv | string? | Base64 16-byte IV. Omit to generate one. |
Mode | string? | Only the exact string CBC is supported. Defaults to CBC. |
Returns
| Type | Description |
|---|---|
string | Base64 ciphertext. |
string | Base64 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.