How to Encrypt and Decrypt with NodeJS | HackerNoon (2024)

How to encrypt text

Create a file named encdec.js and paste:

const crypto = require("crypto")const encrypt = (plainText, password) => { try { const iv = crypto.randomBytes(16); const key = crypto.createHash('sha256').update(password).digest('base64').substr(0, 32); const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); let encrypted = cipher.update(plainText); encrypted = Buffer.concat([encrypted, cipher.final()]) return iv.toString('hex') + ':' + encrypted.toString('hex'); } catch (error) { console.log(error); }}

Let's test it:

Append these lines:

const encrypt = (plainText, password) => { ...}const text = "Hello World"const pass = "secret1234"const encText = encrypt(text, pass)console.log('encrypted text', encText);

Then run:

node encdec.js# Output:encrypted text af9efafd353a5a7e27f31262dac12d6b:eb1dd75ea6c84e25300d5a244138ab3c

How to decrypt the encrypted text

Add the decryption function:

const decrypt = (encryptedText, password) => { try { const textParts = encryptedText.split(':'); const iv = Buffer.from(textParts.shift(), 'hex'); const encryptedData = Buffer.from(textParts.join(':'), 'hex'); const key = crypto.createHash('sha256').update(password).digest('base64').substr(0, 32); const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv); const decrypted = decipher.update(encryptedData); const decryptedText = Buffer.concat([decrypted, decipher.final()]); return decryptedText.toString(); } catch (error) { console.log(error) }}

And a test:

const text = "Hello World"const pass = "secret1234"const encText = encrypt(text, pass)console.log('encrypted text', encText);const decText = decrypt(encText, pass)console.log('decrypted text', decText);

Then run :

node encdec.js# Outputencrypted text 71596b9f5a99532f438fc5669b845680:248f6cb24a4ebeb174bbb73953115fd5decrypted text Hello World

Source: https://gist.github.com/vlucas/2bd40f62d20c1d49237a109d491974eb

Also published on https://alexadam.dev/blog/encrypt-decrypt-in-node.html.

How to Encrypt and Decrypt with NodeJS | HackerNoon (2024)
Top Articles
Make immediate payments with Instant Pay
SE.PAY, More Tip Payment Options With Lower Fees
Cranes For Sale in United States| IronPlanet
Duralast Gold Cv Axle
Mcgeorge Academic Calendar
Danielle Moodie-Mills Net Worth
Instructional Resources
Erika Kullberg Wikipedia
Retro Ride Teardrop
Joe Gorga Zodiac Sign
2021 Lexus IS for sale - Richardson, TX - craigslist
Used Wood Cook Stoves For Sale Craigslist
REVIEW - Empire of Sin
Trini Sandwich Crossword Clue
Seattle Rpz
Craigslist Edmond Oklahoma
Mflwer
Odfl4Us Driver Login
CDL Rostermania 2023-2024 | News, Rumors & Every Confirmed Roster
Indiana Wesleyan Transcripts
Craigslist Pearl Ms
Roane County Arrests Today
Cain Toyota Vehicles
Hctc Speed Test
Delectable Birthday Dyes
Booknet.com Contract Marriage 2
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
Darrell Waltrip Off Road Center
January 8 Jesus Calling
Craigslist Rome Ny
Doctors of Optometry - Westchester Mall | Trusted Eye Doctors in White Plains, NY
Robotization Deviantart
Wolfwalkers 123Movies
Co10 Unr
Mchoul Funeral Home Of Fishkill Inc. Services
Greyson Alexander Thorn
Willys Pickup For Sale Craigslist
Strange World Showtimes Near Regal Edwards West Covina
How to Destroy Rule 34
Waffle House Gift Card Cvs
Weather Underground Bonita Springs
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Best Restaurants Minocqua
Winta Zesu Net Worth
[Teen Titans] Starfire In Heat - Chapter 1 - Umbrelloid - Teen Titans
Ucla Basketball Bruinzone
Bf273-11K-Cl
Automatic Vehicle Accident Detection and Messageing System – IJERT
The Plug Las Vegas Dispensary
Gear Bicycle Sales Butler Pa
The Significance Of The Haitian Revolution Was That It Weegy
Provincial Freeman (Toronto and Chatham, ON: Mary Ann Shadd Cary (October 9, 1823 – June 5, 1893)), November 3, 1855, p. 1
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5628

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.