- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Encrypt and Decrypt Data in NodeJS
NodeJS provides inbuilt library crypto to encrypt and decrypt data in NodeJS. We can use this library to encrypt data of any type. You can do the cryptographic operations on a string, buffer, and even a stream of data. The crypto also holds multiple crypto algorithms for encryption. Please check the official resources for the same. In this article, we will use the most popular AES (Advanced Encryption Standard) for encryption.
Configuring 'crypto' dependency
In your project, check if NodeJS is initialized or not. If not, use the following command to initialize NodeJS.
>> npm init -y
The 'crypto' library is automatically added while installing the node manually. If not, you can use the following command to install crypto.
>> npm install crypto –save
Example
Encrypting and Decrypting Data
//Checking the crypto module const crypto = require('crypto'); const algorithm = 'aes-256-cbc'; //Using AES encryption const key = crypto.randomBytes(32); const iv = crypto.randomBytes(16); //Encrypting text 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') }; } // Decrypting text 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(); } // Text send to encrypt function var hw = encrypt("Welcome to Tutorials Point...") console.log(hw) console.log(decrypt(hw))
Output
C:\Users\mysql-test>> node encrypt.js { iv: '61add9b0068d5d85e940ff3bba0a00e6', encryptedData: '787ff81611b84c9ab2a55aa45e3c1d3e824e3ff583b0cb75c20b8947a4130d16' } //Encrypted text Welcome to Tutorials Point... //Decrypted text
- Related Articles
- How to encrypt and decrypt data in Python
- Encrypt and Decrypt a string in MySQL?
- How to encrypt and decrypt a file using gpg command on linux
- Difference between NodeJS and AngularJS
- Difference between NodeJS and ReactJS
- Difference between console.log and process.stdout.write in NodeJS
- How to encrypt password and store in Android sqlite?
- Decrypt String from Alphabet to Integer Mapping in Python
- Introduction to ensureFileSync() in NodeJS
- Introduction to Sequelize in NodeJS
- How to encrypt a CLOB datatype in JDBC?
- Program to decrypt code to defuse the bomb in Python
- Async Copy in fs-extra - NodeJS
- Deleting records in MySQL using Nodejs
- Difference between process.cwd & _ _dirname in NodeJS

Advertisements