---
title: Encryption
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/encryption/
updated: 2026-11-18T16:57:19.575Z
---

# Encryption

> ENCRYPT(), DECRYPT(), ENCRYPTREQ(), DECRYPTREQ() and the keys $crypto, $cryptoiv and secret.

The functions that encrypt and decrypt values, and the keys that choose the cipher. The guide [Encryption](../../guides/encryption/) explains how they work together with examples.

## API

### ENCRYPT(value, key, [unique])

Type: `method`

Encrypts a value into a string.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Any JSON-serializable value. |
| `key` | `String` | yes |  | A key that must also be used to decrypt. |
| `unique` | `Boolean` | no |  | A random output for the same input. |

Returns: `String` The encrypted value.

`value` is serialized to JSON; `null` or `undefined` gives an empty string. The built-in algorithm uses `CONF.secret` together with `key`; with `unique` the output differs on every call. When `CONF.$crypto` is set, the value is encrypted with that Node.js cipher and `CONF.$cryptoiv`, and `key` must have the byte length of the cipher (`unique` is not used). `F.encrypt()` is the same function.

Example: Reset link
```js
var token = ENCRYPT({ id: user.id, expire: NOW.add('1 day') }, 'reset');
$.success(token);
```

### DECRYPT(value, key, [tojson])

Type: `method`

Decrypts a string made by ENCRYPT().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The encrypted string. |
| `key` | `String` | yes |  | The key used by `ENCRYPT()`. |
| `tojson` | `Boolean` | no | `true` | Parse the result as JSON. |

Returns: `Object|String` The value, or `null`.

Decrypts a string made by `ENCRYPT()` with the same `key`. With `tojson` (default) the result is parsed as JSON; a string that is not valid, was made with another key or does not contain JSON returns `null`. With `tojson: false` you get the decrypted text. When `CONF.$crypto` is set, the value is decrypted with that Node.js cipher, and a value with an odd number of hex characters returns `null` at once. `F.decrypt()` is the same function.

Example: Verify a token
```js
var data = DECRYPT($.query.token, 'reset');
if (!data || data.expire < NOW) {
	$.invalid(400);
	return;
}
```

### ENCRYPTREQ($, value, key, [strict])

Type: `method`

Encrypts a value bound to the client of the request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | yes |  | The `$` of an action or a controller. |
| `value` | `Object` | yes |  | Any JSON-serializable value. |
| `key` | `String` | yes |  | The key. |
| `strict` | `Boolean` | no |  | Bind the value to the IP address too. |

Returns: `String` The encrypted value.

Stores a hash of the `User-Agent` header with the value, and with `strict` also the IP address. `DECRYPTREQ()` returns the value only for the same client. `F.encryptreq()` is the same function.

Example: Download token
```js
var token = ENCRYPTREQ($, { file: model.id }, 'download', true);
$.success(token);
```

### DECRYPTREQ($, value, key)

Type: `method`

Decrypts a value made by ENCRYPTREQ() for the same client.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | yes |  | The `$` of an action or a controller. |
| `value` | `String` | yes |  | The encrypted string. |
| `key` | `String` | yes |  | The key used by `ENCRYPTREQ()`. |

Returns: `Object` The value, or `undefined`.

Returns `undefined` when the value is empty, invalid, or comes from another browser (or another IP address for a strict value). `F.decryptreq()` is the same function.

Example: Check the token
```js
var data = DECRYPTREQ($, $.query.token, 'download');
if (!data) {
	$.invalid(401);
	return;
}
```

### $crypto : aes-256-cbc

Type: `config`

A Node.js cipher for ENCRYPT() and DECRYPT().

Returns: `String`

Any algorithm of `crypto.createCipheriv()`. Without it the built-in algorithm is used. Keys must then have the byte length of the cipher (32 characters for `aes-256-cbc`).

Example: Config
```js
// /config
$crypto      : aes-256-cbc
$cryptoiv    : 00112233445566778899aabbccddeeff
```

### $cryptoiv : <hex or base64>

Type: `config`

The initialization vector for $crypto.

Returns: `Buffer`

Read as base64 when it contains upper-case letters, `=`, `/` or `+`, otherwise as hex. The default is the first 16 bytes of the application hash.

Example: Config
```js
$cryptoiv : 00112233445566778899aabbccddeeff
```

### secret : <string>

Type: `config`

The application secret used by ENCRYPT() and DECRYPT().

Returns: `String`

It is mixed into the key of the built-in algorithm, so values encrypted with another secret do not decrypt. It has no default (`CONF.secret` is `undefined`), so set your own; the same secret keeps tokens valid across servers.

Example: Config
```js
secret : a-long-random-string
```
