---
title: Cifratura
project: Total.js V5
version: 0.0.20
section: guides
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v5/0-0-20/guides/encryption/
updated: 2026-11-16T15:32:32.495Z
---

# Cifratura

> Cifra i valori con ENCRYPT() e DECRYPT(), legali al client di una richiesta con ENCRYPTREQ() e DECRYPTREQ() e scegli il cifrario.

`ENCRYPT(value, key)` trasforma qualsiasi valore serializzabile in JSON in una stringa e `DECRYPT(string, key)` lo riconverte, o restituisce `null` per una stringa non valida. Il segreto `CONF.secret` fa parte della chiave.

```js
var token = ENCRYPT({ id: user.id, expire: NOW.add('1 day') }, 'reset');
var data = DECRYPT(token, 'reset');
```

`ENCRYPTREQ($, value, key)` e `DECRYPTREQ($, value, key)` legano il valore al browser della richiesta (un hash di `User-Agent`, e dell'indirizzo IP con `strict`), così un token copiato su un altro client non viene decifrato:

```js
var token = ENCRYPTREQ($, { id: user.id }, 'download');
// later, in another request of the same browser
var data = DECRYPTREQ($, token, 'download');
```

Per usare un cifrario di Node.js invece dell'algoritmo integrato, imposta `$crypto` nella configurazione (per esempio `aes-256-cbc`). La chiave deve allora avere la lunghezza in byte richiesta dal cifrario.
