In this Article, We’re going to see. and decrypt it using that same with Node.js built-in library called 'crypto'. when necessary.

When it comes to storing anything sensitive, whether it be data or something else, you must do so safely and securely. For example, the Register/Login data must be encrypted first and then decrypted when used.

SOURCE CODE DOWNLOAD For Demo & Source Code Scroll down.

#

We’re going to create a fresh project to work with Crypto where you will see how to encrypt & decrypt data. To start, Execute the following command:

npm init -y

The above command will create a new package.json file and initialize our project. We’re going to be making use of the Node.js Crypto library for any and all cipher and decipher logic.

To install the dependency, execute the following from the command line:

npm install crypto --save

Finally, we need to start build our project. We have all necessary files to be used while making this. For project cleanliness, we’re going to create a Functions for all encryption and decryption that will instantiate. When we need to Encrypt or Decrypt our data

#

We’re going to be using modern JavaScript for this example which means we’re going to create an ES6 class for our encryption and decryption logic.

Create & Open the project’s crypt.js file and include following code:

const crypto = require('crypto'); const secret = 'appSecretKey'; const rounds = 9921; const keySize = 32; const algorithm = 'aes-256-cbc'; const salt = crypto.createHash('sha1').update(secret).digest("hex");

Here, we’re going to be offering synchronous and asynchronous methods for encrypting and decrypting a file. Also we define above that we going to use aes-256-cbc algorithm method.

Now, if we want to encrypt a JavaScript object on disk. If we wanted to do this synchronously, we would look at the encrypt function below:

function encryptData(data) { try { let iv = crypto.randomBytes(16); let key = crypto.pbkdf2Sync(secret, salt, rounds, keySize, 'sha512'); let cipher = crypto.createCipheriv(algorithm, Buffer.from(key), iv); let encryptedData = Buffer.concat([cipher.update(JSON.stringify(data)), cipher.final()]); return iv.toString('base64') + ':' + encryptedData.toString('base64'); } catch (err) { console.error(err) return false; } }

In the above coed, we define that we want to use an AES algorithm. Because we want to save to a file, we want to create a buffer from our plaintext data. Once we have an encrypted buffer, we can write to the file system & return a message.

We’re creating a cipher within the function. After we call cipher.final() we are not able to use the cipher in future. Because of this, we’ll get random results if we create a class variable for it and try to use it multiple times.

To decrypt this file, we would call the decrypt method:

function decryptData(encData) { try { let textParts = encData.split(':'); let iv = Buffer.from(textParts.shift(), 'base64'); let encryptedData = Buffer.from(textParts.join(':'), 'base64'); let key = crypto.pbkdf2Sync(secret, salt, rounds, keySize, 'sha512'); let decipher = crypto.createDecipheriv(algorithm, Buffer.from(key), iv); let decryptedData = decipher.update(encryptedData); decryptedData = Buffer.concat([decryptedData, decipher.final()]); return JSON.parse(decryptedData.toString()); } catch (err) { console.error(err) return false; } }

In the above function we are reading the file into a buffer, decrypting it with the AES algorithm, and returning the decrypted object back to the user.

#

var dataObj = {}; dataObj.email = 'user@example.com'; dataObj.id = 2451; var encrytedText = encryptData(dataObj); console.log(`Encrypted text - ${encrytedText}`); console.log(`Decrypted text: `); console.log(decryptData(encrytedText));

You just saw how to encrypt and decrypt data with the Node.js Crypto library. This is very useful & secure when you need to encrypt sensitive data. For example, let’s say we wanted to create an Register/login Application & want to save Username & Password. We could in this scenario & this to just remove all security issue.

You will get all files, when you download the source code. And after than you can edit it according to you

if you face any issues you can contact by asking question with article link.

You can go through Demo as well as Download source code for the same & make changes according to you