---
title: Encryption
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/encryption/
updated: 2026-11-18T16:57:34.578Z
---

# Encryption

> ENCRYPT(), DECRYPT(), ENCRYPTREQ(), DECRYPTREQ() and CONF.secret.

The functions that encrypt and decrypt values, and the secret they use. The guide [Encryption](../../guides/encryption/) explains how to use them with examples; this page is the exact reference.

## API

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

Type: `method`

Encrypts a value with CONF.secret and a key.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object|String|Number` | yes |  | The value. |
| `key` | `String` | no |  | Your key, combined with `CONF.secret`. |
| `isUnique` | `Boolean` | no |  | Different output for the same input. |

Returns: `String` The encrypted value; an empty string for `null`.

Objects are serialized to JSON, numbers to strings, and a function is called for its value. The result is a string you can store in a cookie or a URL. When `CONF.default_crypto` is set to a Node.js cipher name, the value is encrypted with that cipher, `key` as the cipher key and `CONF.default_crypto_iv`.

Example: Encrypt an object
```js
var token = ENCRYPT({ id: user.id, expire: NOW.add('1 day') }, 'reset');
```

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

Type: `method`

Decrypts a value from ENCRYPT().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The encrypted value. |
| `key` | `String` | no |  | The same key as in `ENCRYPT()`. |
| `jsonConvert` | `Boolean` | no | `true` | Parse the result as JSON. |

Returns: `Object|String|null`

By default the result is parsed as JSON and `null` is returned when it is not JSON, so pass `false` as `jsonConvert` for a plain string. Returns `null` when the value cannot be decrypted.

Example: Decrypt
```js
var data = DECRYPT(self.query.token, 'reset');
if (!data || data.expire < NOW)
	return self.invalid('error-token');
```

Example: Plain string
```js
var id = DECRYPT(value, 'key', false);
```

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

Type: `method`

Encrypts a value bound to the browser of the request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `req` | `Request|Controller` | yes |  | The request. |
| `value` | `Object|String` | yes |  | The value. |
| `key` | `String` | no |  | Encryption key. |
| `strict` | `Boolean` | no | `false` | Bind to the IP address too. |

Returns: `String` The token.

Stores the user agent (and with `strict` the IP address) next to `value` and encrypts it with `ENCRYPT()`. `DECRYPTREQ()` returns the value only for a request with the same user agent (and IP address). `req` can be a request or a controller.

Example: Sign-in cookie
```js
self.cookie('token', ENCRYPTREQ(self, user.id, 'auth'), '1 month');
```

### DECRYPTREQ(req, value, [key])

Type: `method`

Decrypts a value from ENCRYPTREQ() for the same browser.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `req` | `Request|Controller` | yes |  | The request. |
| `value` | `String` | yes |  | The token. |
| `key` | `String` | no |  | The same key as in `ENCRYPTREQ()`. |

Returns: `Object|String|undefined`

Returns the value, or `undefined` when it cannot be decrypted, or the user agent (or the IP address in strict mode) differs from the request.

Example: In AUTH()
```js
var id = DECRYPTREQ($.req, $.cookie('token'), 'auth');
```

### secret : your-secret

Type: `config`

The secret of the app used by ENCRYPT() and DECRYPT().

Returns: `String`

Defaults to a hash generated for the app. Set your own value in `/config` so tokens stay valid when the app moves to another server. Changing it makes all encrypted tokens invalid.

Example: config
```js
// /config
secret : a-long-random-secret
```
