Encrypting Data in Node.js Last Updated : 11 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Encryption and Decryption in Node can be done by installing and implementing the 'crypto' library. If you have installed Node.js by manual build, then there is a chance that the crypto library is not shipped with it. You can run the following command to install the crypto dependency. npm install crypto --save But you don’t need to do that if you have installed it using pre-built packages. Example for implementing crypto in Node. javascript // Nodejs encryption 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(); } let gfg = encrypt("GeeksForGeeks"); console.log(gfg); console.log(decrypt(gfg)); Output: Comment More infoAdvertise with us Next Article Encrypting Data in Node.js N NishanthVaidya Follow Improve Article Tags : Web Technologies Node.js Similar Reads JWT Authentication In Node.js In modern web development, ensuring secure and efficient user authentication is paramount. JSON Web Tokens (JWT) offer a robust solution for token-based authentication, enabling secure transmission of user information between parties. This article provides a step-by-step approach to implementing JWT 3 min read Encrypt and Protect Data in MongoDB As technology advances so securing sensitive data is increasingly important for organizations. MongoDB a popular NoSQL database that supports strong encryption to protect data from unauthorized access. In this article, We will learn about how to encrypt data in MongoDB by including data in transit w 5 min read Encryption at Rest - MongoDB Encryption at rest is a critical security feature that protects stored data from unauthorized access and breaches. MongoDB provides encryption at rest to safeguard data when it is stored on disk, ensuring that even if an attacker gains access to physical storage, the data remains unreadable without 4 min read How to Encrypt MongoDB Data? In todayâs digital world, keeping your sensitive data safe is crucial. MongoDB, a top player in modern data management, offers various ways to encrypt your data. In this article, we will explore MongoDB encryption techniques, including encryption at rest, encryption in transit, and client-side encry 6 min read HTTPS in Node.js HTTP: When the data is transferred in HTTP protocol it just travels in the clear text format. HTTPS: It simply encrypts the request from the browser to the web server, so it is tough to sniff that information. It basically works on two things: SSL (Secure Socket Layer)TLS (Transport layer security 3 min read Like