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#
crypt.encrypt(data: string, key: string, iv: string?, mode: string?): (string, string)Parameters#
| Parameter | Type | Description |
|---|---|---|
data | string | Raw plaintext bytes. |
key | string | Base64 encoding of exactly 32 bytes. |
ivoptional | string | Base64 IV containing at least 16 decoded bytes; omitted or nil generates a random IV. Only the first 16 bytes are used. |
modeoptional | string | Only 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
local key = crypt.generatekey()
local ciphertext, iv = crypt.encrypt("Local note", key)
print(crypt.decrypt(ciphertext, key, iv)) -- Local noteNotes#
- Keep your key private and save the returned IV for decryption. Encryption alone does not authenticate the data.