NodeJS Data encryption description using crypto js library
NodeJS Data encryption description using crypto js library

Hi Guys, Welcome to Proto Coders Point, Data Encrypting & Data Decryption is must requirement when it come to data security in any application development may be it Web Application, Mobile Application of any other Application. Especially when data or information that is transmit is sensitive and can’t be directly readable by the app users.

To make the data security high it’s always recommended to better apply an encryption and decryption during data transmission from server to client.

Video Tutorial

NodeJS Data Encryption & Decryption using Crypto-JS NodeJS Module

There are various NodeJS Module by which NodeJS Developer can achieve data encryption & decryption within the application. Will make use of popular and most commonly used module/library called 'crypto-js'.

Below is a simple example how to make use of 'crypto-js' module for encryption & decryption of data in NodeJS. For this example will create a NodeJS Express server and create 2 API routes, one for encryption and another for decryption.

Necessary Packages Required:

We need 4 dependencies modules for Implementation

  1. body-parser: Used as a middleware for parsing incoming request bodies in Express.
  2. cors: Middleware for enabling Cross-Origin Resource Sharing in web applications.
  3. express: Web Application framework of NodeJS used for creating NodeJS Server.
  4. crypto-js: is a JavaScript library used for data encryption & decryption using cryptographic functions.

Install them using below cmd

npm install body-parser cors express crypto-js

How to make use of NodeJS Crypto-JS to encrypt data

function encrypt(data,key){
    const cipherText = CryptoJS.AES.encrypt(data,key).toString();
    return cipherText;
}

The above function accept 2 parameter i.e data & key. Here data is the msg or information that you want to encrypt and key is basically a secret key that is been used by CryptoJS to encrypt that data.

The CryptoJS loibrary in NodeJS making use of Advanced Encryption Standard (AES) algorithm to encrypt the data.

The key is very important as it used for both data encryption & decryption.


How to decrypt data in NodeJS using CryptoJS

To decrypt data in nodeJS we need encrypted format of data and the key that was used to encrypt the data.

function decrypt(cipherText,key){
    try {
        const bytes = CryptoJS.AES.decrypt(cipherText,key);

        if(bytes.sigBytes > 0){
            const decryptedData = bytes.toString(CryptoJS.enc.Utf8);
            return decryptedData;
        }else{
            throw new Error('Decryption Failed Invalid Key')
        }
    } catch (error) {
        throw new Error('Decryption Failed Invalid Key')
    }

}

in above example cipherText means encrypted data and key is used to decrypt the data.


Complete Source Code – NodeJS Data Encryption & Description using CryptoJS library

app.js

Here we have 2 API Routes, encrypt & decrypt and as the make of this API itself says that it can we used for data encryption & decryption.

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const CryptoJS = require('crypto-js');

const app = express();

app.use(cors());

app.use(bodyParser.json({limit:'50mb'}))
app.use(bodyParser.urlencoded({limit:'50mb',extended:true}));


app.get('/',(req,res)=>{
    res.send(" Say Hello To Proto Coders Point, Please Subscribe")
});


function encrypt(data,key){
    const cipherText = CryptoJS.AES.encrypt(data,key).toString();
    return cipherText;
}


function decrypt(cipherText,key){
    try {
        const bytes = CryptoJS.AES.decrypt(cipherText,key);

        if(bytes.sigBytes > 0){
            const decryptedData = bytes.toString(CryptoJS.enc.Utf8);
            return decryptedData;
        }else{
            throw new Error('Decryption Failed Invalid Key')
        }
    } catch (error) {
        throw new Error('Decryption Failed Invalid Key')
    }

}

app.post('/encrypt',(req,res)=>{
    const { data, key} = req.body;
    const encrypted = encrypt(data,key);
    res.json({encrypted});
});

app.post('/decrypt',(req,res)=>{
    const { encryptedData, key } = req.body;

    const decryptedData = decrypt(encryptedData, key);
    res.json({decryptedData});
});


module.exports = app;

index.js

This is the starting point of our NodeJS Project

start the nodeJS server using below cmd

node index.js