We will share with you in this article how to encrypt and decrypt data in node.js using crypto. Node.js provides the in-built library crypto
for data encrypt and data decrypt. you can do any cryptographic opration help of crypto
library on any srting, buffer and stream. as you know that very well data security in very important in web applications.
In this article, we will show you examples of how you can do these encrypt and decrypt operations on data in your node.js applications.
For a data encrypt and decrypt you can use multiple crypto
algorithms. check out here official node.js docs link Crypto in Node.js.
We will use AES (Advanced Encryption System)
crypto algorithms in this article for data encrypt and decrypt.
Step - 1 Create a new node.js project
First, we need to create one Node.js application in our system. you can create fresh new node.js application anywhere in your system then go to the node.js application's root path and create pakcage.js
file help of the following command.
npm init -y
Step - 2 Install Package (Optional)
This step is optional if you have installed a new version of Node.js. crypto
has in-built in the new Node.js version.
Now, the second thing is an install crypto
package in your Node.js application. you can install crypto
package help of run the following command in your project's root directory.
npm install crypto --save
Step - 3 Encrypt and decrypt data (Strings, numbers etc)
Now we will see some example demo code of how to encrypt and decrypt on string and numbers. you can check the following examples. just put the following code in your index.js
file.
// Nodejs encryption examples with CTR
const crypto = require('crypto');
const algorithm = 'aes-256-cbc';
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
function encrypt(text) {
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
}
function decrypt(text) {
let iv = Buffer.from(text.iv, 'hex');
let encryptedText = Buffer.from(text.encryptedData, 'hex');
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
var hw = encrypt("Let's do this encrypt and decrypt.")
console.log(hw)
console.log('Your decrypt string is : '+ decrypt(hw))
Below example, we just create two functions one for encrypt data and a second one for decrypt data. now you can run and see the output help of run the following command.
node index.js
Here is the output:
Your output looks like in your terminal window.
{
iv: '5367a0bc7b47ade7862925398e4e8811',
encryptedData: '8295c8028c6c823ef0a475fae424c07b975c60400ddb079f2b326e8f49108d70ba5839120bd3725956bdb18f2ecd7df6'
}
Your decrypt string is : Let's do this encrypt and decrypt.
Step - 4 Encrypt and decrypt buffer
Now, we will see how to work encrypt and decrypt on buffer. we can use our below encrypt and decrypt function also in buffer operations. just here one example.
Like this.
var hw = encrypt(Buffer.from("Some serious stuff","utf-8"))
console.log(hw)
console.log(decrypt(hw))
Conclusion
As you can see, encrypt and decrypt data in Node.js it is very easy to use and implement in Node.js application help of crypto
.
We hope that small tutorials help everyone.