A simple way to encrypt and decrypt JSON, creating secure data transfer
About • Installation • Usage • Authors • License
Encrypt and decrypt JSON or Simple JavaScript objects. Designed to be used as middleware between the backend and the frontend to prevent web users from getting a human readable JSON, directly in the browser. Useful for frontend applications with logic and/or data processing.
For now, JSON-Crypto can encrypt and decrypt any JSON. But for JavaScript Objects, the available key values must be of type: Object
, Array
, String
, Number
, Boolean
, ObjectId
or null
. We're calling this a Simple JavaScript Object
. In this case you cannot have a new Date()
as a value, for example.
Use your favorite JavaScript package manager!
$ npm install @danilo_pereira/json-crypto
$ yarn add @danilo_pereira/json-crypto
Perhaps, the dependecies, like browser-or-node
, crypto
, crypto-js
and bson
dependency must be installed manually for the project to work.
$ npm install <DEPENDENCY>
or
$ yarn add <DEPENDENCY>
Once installed, you can import JSON-Crypto
into your JavaScript project, insert at the top of your file the following line:
import jsonCrypto from '@danilo_pereira/json-crypto';
or
var jsonCrypto = require('@danilo_pereira/json-crypto');
You also can import the methods one by one, using:
import { encrypt, decrypt, encryptKey, decryptKey } from "@danilo_pereira/json-crypto";
In the latter case, you should not call json-crypto
methods like jsonCrypto.encrypt(...)
but use the method directly like encrypt(...)
To encrypt a JSON or Simple JavaScript Object you should call jsonCrypto.encrypt(json, encriptedKeys, secret, secret_2)
.
- json: REQUIRED A input JSON or Object that should be encrypted. Should be of type
JSON
orObject
. - encriptedKeys: OPTIONAL Define if Object keys will be also encrypted. Should be of type
Boolean
. The default value isfalse
. - secret: OPTIONAL Secret hex string (with up to 32 chars), used as hash to encrypt. Should be of type
String
. We use a default hash if not informed. - secret_2: OPTIONAL Secret hex string (with up to 16 chars), also used as hash to encrypt. Should be of type
String
. We use a default hash if not informed.
This should return a valid Simple JavaScript Object encrypted.
import jsonCrypto from '@danilo_pereira/json-crypto';
...
// SIMPLE CASE:
jsonCrypto.encrypt({
key_1: "value_1",
key_2: 2,
key_3: null,
key_4: false,
key_5: { key_5_1: "value_5" },
key_6: ["value_6_1", "value_6_2"],
});
/*
Output:
{
key_1: '126b1c3b908fc3',
key_2: '1073002bd6be87660199ef1043',
key_3: '1073002bd6be87670f',
key_4: '1073002bd6b29d640f99fc444bb14f69ce5c',
key_5: { key_5_1: '2c6b0326a3b19e7e06a3a8' },
key_6: [ '126b1c3b908fc45452', '126b1c3b908fc45451' ]
}
*/
// KEY ENCRYPT CASE:
jsonCrypto.encrypt({
key_1: "value_1",
key_2: 2,
key_3: null,
key_4: false,
key_5: { key_5_1: "value_5" },
key_6: ["value_6_1", "value_6_2"],
}, true);
/*
Output:
{
'0f6f0911c4': '126b1c3b908fc3',
'0f6f0911c7': '1073002bd6be87660199ef1043',
'0f6f0911c6': '1073002bd6be87670f',
'0f6f0911c1': '1073002bd6b29d640f99fc444bb14f69ce5c',
'0f6f0911c0': { '0f6f0911c08fc3': '2c6b0326a3b19e7e06a3a8' },
'0f6f0911c3': [ '126b1c3b908fc45452', '126b1c3b908fc45451' ]
}
*/
// KEY ENCRYPT CASE, WITH OPTIONAL HASH:
jsonCrypto.encrypt({
key_1: "value_1",
key_2: 2,
key_3: null,
key_4: false,
key_5: { key_5_1: "value_5" },
key_6: ["value_6_1", "value_6_2"],
}, true, "cad63375bd783382", "a92e070ecd301b6de99a8985f8e9cf9f");
/*
Output:
{
'16de1fe44c': '0bda0ace187253',
'16de1fe44f': '09c216de5e43174cbf5b8cd709',
'16de1fe44e': '09c216de5e43174db1',
'16de1fe449': '09c216de5e4f0d4eb15b9f830178d87bc7ff',
'16de1fe448': { '16de1fe4487253': '35da15d32b4c0e54b861cb' },
'16de1fe44b': [ '0bda0ace1872547eec', '0bda0ace1872547eef' ]
}
In this case, the secret was defined at: "cad63375bd783382####################" (32 chars) and secret_2 was defined at: "a92e070ecd301b6d" (16ch)
*/
To decrypt a JSON or Simple JavaScript Object you should call jsonCrypto.decrypt(json, encriptedKeys, secret, secret_2)
.
- json: REQUIRED A input JSON or Object that should be decrypted. Should be of type
JSON
orObject
. - encriptedKeys: OPTIONAL Define if Object keys will be also decrypted. Should be of type
Boolean
. The default value isfalse
. - secret: OPTIONAL Secret hex string (with up to 32 chars). Needs to be the same secret hash used to encrypt. Should be of type
String
. We use a default hash if not informed. - secret_2: OPTIONAL Secret hex string (with up to 16 chars). Needs to be the same secret hash used to encrypt. Should be of type
String
. We use a default hash if not informed.
This should return a valid Simple JavaScript Object decrypted.
import jsonCrypto from '@danilo_pereira/json-crypto';
...
// SIMPLE CASE:
jsonCrypto.decrypt(
{
key_1: '126b1c3b908fc3',
key_2: '1073002bd6be87660199ef1043',
key_3: '1073002bd6be87670f',
key_4: '1073002bd6b29d640f99fc444bb14f69ce5c',
key_5: { key_5_1: '2c6b0326a3b19e7e06a3a8' },
key_6: [ '126b1c3b908fc45452', '126b1c3b908fc45451' ]
});
/*
Output:
{
key_1: "value_1",
key_2: 2,
key_3: null,
key_4: false,
key_5: { key_5_1: "value_5" },
key_6: ["value_6_1", "value_6_2"],
}
*/
// KEY ENCRYPT CASE:
jsonCrypto.decrypt(
{
'0f6f0911c4': '126b1c3b908fc3',
'0f6f0911c7': '1073002bd6be87660199ef1043',
'0f6f0911c6': '1073002bd6be87670f',
'0f6f0911c1': '1073002bd6b29d640f99fc444bb14f69ce5c',
'0f6f0911c0': { '0f6f0911c08fc3': '2c6b0326a3b19e7e06a3a8' },
'0f6f0911c3': [ '126b1c3b908fc45452', '126b1c3b908fc45451' ]
}, true);
/*
Output:
{
key_1: "value_1",
key_2: 2,
key_3: null,
key_4: false,
key_5: { key_5_1: "value_5" },
key_6: ["value_6_1", "value_6_2"],
}
*/
// KEY ENCRYPT CASE, WITH OPTIONAL HASH:
jsonCrypto.decrypt({
'16de1fe44c': '0bda0ace187253',
'16de1fe44f': '09c216de5e43174cbf5b8cd709',
'16de1fe44e': '09c216de5e43174db1',
'16de1fe449': '09c216de5e4f0d4eb15b9f830178d87bc7ff',
'16de1fe448': { '16de1fe4487253': '35da15d32b4c0e54b861cb' },
'16de1fe44b': [ '0bda0ace1872547eec', '0bda0ace1872547eef' ]
}, true, "cad63375bd783382", "a92e070ecd301b6de99a8985f8e9cf9f");
/*
Output:
{
key_1: "value_1",
key_2: 2,
key_3: null,
key_4: false,
key_5: { key_5_1: "value_5" },
key_6: ["value_6_1", "value_6_2"],
}
In this case, the secret was defined at: "cad63375bd783382####################" (32 chars) and secret_2 was defined at: "a92e070ecd301b6d" (16ch)
*/
To decrypt a String
you should call jsonCrypto.decryptKey(string, secret, secret_2)
.
- string: REQUIRED A input string that should be decrypted. Should be of type
string
. - secret: OPTIONAL Secret hex string (with up to 32 chars). Needs to be the same secret hash used to encrypt. Should be of type
String
. We use a default hash if not informed. - secret_2: OPTIONAL Secret hex string (with up to 16 chars). Needs to be the same secret hash used to encrypt. Should be of type
String
. We use a default hash if not informed.
This should return a String
, Number
, Boolean
or null
decrypted.
import jsonCrypto from '@danilo_pereira/json-crypto';
...
// SIMPLE CASE:
jsonCrypto.decryptKey("126b1c3b908fc3");
/*
Output:
"value_1"
*/
To encrypt a String
you should call jsonCrypto.encryptKey(string, secret, secret_2)
.
- string: REQUIRED A input string that should be encrypted. Should be of type
string
. - secret: OPTIONAL Secret hex string (with up to 32 chars). Needs to be the same secret hash used to encrypt. Should be of type
String
. We use a default hash if not informed. - secret_2: OPTIONAL Secret hex string (with up to 16 chars). Needs to be the same secret hash used to encrypt. Should be of type
String
. We use a default hash if not informed.
This should return a String
, Number
, Boolean
or null
decrypted.
import jsonCrypto from '@danilo_pereira/json-crypto';
...
// SIMPLE CASE:
jsonCrypto.decryptKey("126b1c3b908fc3");
/*
Output:
"value_1"
*/
Danilo Pereira Author |
This project is under the GNU General Public License.
Documentation Template made by:
LetÃcia Vigna Documentation template author |