crypt.decrypt

Decrypts data using a specified algorithm and key.

Syntax

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

Parameters

ParameterTypeDescription
datastringBase64-encoded ciphertext returned by crypt.encrypt
keystringBase64-encoded 32-byte AES key
ivstringBase64-encoded IV returned by crypt.encrypt
modestring?CBC, ECB, CTR, CFB, OFB, or GCM (default: CBC)

Returns

TypeDescription
stringThe decrypted data

Description

crypt.decrypt decrypts data returned by crypt.encrypt. The key, IV, and mode must match the encryption call. GCM verifies the appended authentication tag and raises an error if verification fails.

Example

local key = crypt.generatekey()
local original = "Hello, World!"

-- Encrypt and decrypt
local encrypted, iv = crypt.encrypt(original, key, nil, "CBC")
local decrypted = crypt.decrypt(encrypted, key, iv, "CBC")

print(decrypted) -- "Hello, World!"