{encrypt:text:key:output_type}

Description

Encrypts text with a password using authenticated AES-256-GCM encryption (via libsodium). The key can be any length - it is derived into a 32-byte AES key. The result is a URL-safe Base64url string with a fresh random nonce prepended, so encrypting the SAME text twice gives DIFFERENT output every time; encrypt is therefore not deterministic and is meant to be paired with decrypt using the same key. Because it is authenticated, decrypting with a wrong key fails and returns an empty string rather than garbage. The third parameter output_type is obsolete and ignored (output is always Base64url now). This no longer uses the old mcrypt extension, so text encrypted by very old AA versions cannot be read back.

Parameters

text required default (required)

The plaintext to encrypt. Any string; an empty string is allowed (it still yields a valid token).

key required default (required)

The password used as the encryption key. Any length is accepted - it is derived into a fixed 32-byte AES-256 key. You must pass the exact same key to decrypt; a wrong key makes decrypt fail and return an empty string.

output_type optional default (none - parameter is ignored)

Obsolete and ignored. In older AA this was set to url to request URL-safe output; today the result is ALWAYS URL-safe Base64url, so this parameter has no effect. Leave it out.

Examples

test{decrypt:{encrypt:Hello world:my-secret-key}:my-secret-key}
ExpectedHello world
ActualHello world
Encryption alone is not testable (the output changes every call). Pair encrypt with decrypt using the SAME key to recover the original text - this round-trip is deterministic.
test{decrypt:{encrypt:42:pass123}:pass123}
Expected42
Actual42
The text can be anything, including digits. Same key in, same text out.
test[{decrypt:{encrypt::pass123}:pass123}]
Expected[]
Actual[]
Encrypting an empty string still produces a valid token (a nonce plus authentication tag); decrypting it returns the empty string. Brackets added to make the empty result visible.
test[{decrypt:{encrypt:secret message:right-key}:wrong-key}]
Expected[]
Actual[]
AES-GCM is authenticated: decrypting with the wrong key does not return garbage, it fails and yields an empty string. Brackets added to make the empty result visible.
virtual{encrypt:Hello world:my-secret-key}
Expected(a Base64url string, e.g. jOa3Fwduj5A3Pdh7FLchC0y1r2fADRptia_vHX-315QgFcjz - different on every call)
ActualxSt6iQ0uyAn_6o0BjOWf_vuHDu0op1lqMhod3bXSPUC9GlZl
Calling encrypt on its own returns a Base64url string (URL-safe: letters, digits, dash and underscore). A fresh random nonce is prepended each call, so the SAME input gives a DIFFERENT ciphertext every time - which is why a bare encrypt cannot be a stable test.
virtual{encrypt:user-42:server-secret}
Expected(a Base64url token, e.g. Kx3pTb9w...; pass it back through decrypt with server-secret to recover user-42)
Actualj8NGH7xVDgBH2TIBOQtchAKDqKixtkhu40f8mCAZQUw
A common real-world use: encrypt an identifier with a server-side secret to make a tamper-proof token to put in a URL or hidden field, then decrypt it later with the same key to read the id back. The token is already URL-safe Base64url.