NcryptJs
NcryptJs is a light weight javascript data encryption and decryption library. This library implements the nodejs default crypto functionality as a mid-channel cipher in addition to a simple and elegant custom data encoding and encryption algorithm.
Contents
Getting Started
This library is available through these javascript node package manager npm and yarn.
Installation
To use this library, first ensure you have a package manager initialized either with npm or yarn
npm install --save ncrypt-js
yarn add ncrypt-js
To include ncrypt-js in your project. use one of these:
import ncrypt from "ncrypt-js";
import * as ncrypt from "ncrypt-js";
import { encrypt, decrypt } from "ncrypt-js";
However, if you are using ECMAScript 5 and older, use the require statement:
var ncrypt = require("ncrypt-js");
var { encrypt, decrypt } = require("ncrypt-js");
Documentation
NcryptJs is a simple library with only two two exposed functions. This is all intentional mainly to keep everything simple. This is a complete documentation of the library and how to use it in your project. All examples work on both ECMAScript 6 (and later) and ECMAScript 5 (and older).
NcryptJs Functions
List of NcryptJs functions.
Functions | Description | Parameters | Return |
---|
encrypt() | Encrypts data. | data: object/string/number/boolean - The data to be encrypted. secret: string - The secret (key or password) that will be used as the salt for the encryption process. | ciphered: string - encrypted data. |
decrypt() | Decrypts the encrypted or ciphered data | encodedData: string - The encrypted data: string to be decrypted. | data: string/object/number/boolean - The decrypted or original data (it might be string or object, depends on the initial input data type). |
Using encrypt()
and decrypt()
functons
To encrypt and decrypt data, simply use encrypt()
and decrypt()
functions respectively. This will use AES-256-CBC
encryption algorithm as the mid-channel cipher.
var { encrypt, decrypt } = require("ncrypt-js");
var data = "Hello World!";
var _secretKey = "some-super-secret-key";
var encryptedData = encrypt(data, _secretKey);
console.log("Encryption process...");
console.log("Plain Text : " + data);
console.log("Cipher Text : " + encryptedData);
var decryptedData = decrypt(encryptedData);
console.log("... and then decryption...");
console.log("Decipher Text : " + decryptedData);
console.log("... done.");
Using default imports
var ncrypt = require("ncrypt-js");
var data = "Hello World!";
var _secretKey = "some-super-secret-key";
var encryptedData = ncrypt.encrypt(data, _secretKey);
console.log("Encryption process...");
console.log("Plain Text : " + data);
console.log("Cipher Text : " + encryptedData);
var decryptedData = ncrypt.decrypt(encryptedData);
console.log("... and then decryption...");
console.log("Decipher Text : " + decryptedData);
console.log("... done.");
Object Encryption
Encryption and decryption of JavaScript object literal has never been simpler than this.
To encrypt and decrypt JavaScript object literal, simply use encrypt()
and decrypt()
function from an instance. This will use AES-CBC encryption algorithm.
var ncrypt = require("ncrypt-js");
var _secretKey = "some-super-secret-key";
var object = {
NycryptJs: "is cool and fun.",
You: "should try it!"
}
var encryptedObject = ncrypt.encrypt(object, _secretKey);
console.log("Encryption process...");
console.log("Plain Object : " + object);
console.log("Encrypted Object : " + encryptedObject);
var decryptedObject = ncrypt.decrypt(encryptedObject);
console.log("... and then decryption...");
console.log("Decipher Text : " + decryptedObject);
console.log("... done.");
Built With
Written in TypeScript, built into ECMAScript 5 using the TypeScript compiler.
Contribution
To contribute, simply fork this project, and issue a pull request.
Version Management
We use SemVer for version management. For the versions available, see the tags on this repository.
Authors
- Chukwuemeka Ajima - Initial work - ajimae
License
This project is licensed under the MIT License - see the LICENSE file for details
Acknowledgments
- This library was developed to simplify how data is encrypted and decrypted in javascript.
- Made available by open source and special thanks to Shahid for his super simple article on node core encryption (crypto) library.
- Thanks to danang-id whose README was very insightful and Jorgeblom for his custom cipher algorithm on this stackoverflow answer