crypt.encrypt
Encrypts a message with AES and returns Base64 ciphertext plus the IV.
Syntax
crypt.encrypt(data: string, key: string, iv: string?, mode: string?) -> (string, string)
Parameters
| Parameter | Type | Description |
|---|---|---|
data | string | Plaintext bytes. |
key | string | Base64-encoded AES key. |
iv | string? | Base64 IV; generated when omitted. |
mode | string? | AES mode; defaults to CBC. |
Returns
| Type | Description |
|---|---|
string | Base64 ciphertext. |
string | Base64 IV. |
Description
The key must decode to 16, 24 or 32 bytes. Modes are CBC, ECB, CTR, CFB, OFB and GCM; CBC is the compatibility default. Omitting the IV generates a fresh one. Retain both returned values. GCM uses a 12-byte generated IV and appends a 16-byte authentication tag before Base64 encoding. Other IV-based modes use the first 16 decoded IV bytes. Non-GCM modes do not authenticate the message.
Example
local key = crypt.generatekey()
local encrypted, iv = crypt.encrypt("hello", key, nil, "GCM")
print(crypt.decrypt(encrypted, key, iv, "GCM"))
These are Game executor contracts. Check build requirements and session lifetimes before using them.
