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
Ask Eli: Property Types Appreciate Faster? | Eli Residential Group
Worst States to Be a Real Estate Agent - Real Estate License Wizard
Craigslist Livingston Montana
11 beste sites voor Word-labelsjablonen (2024) [GRATIS]
Artem The Gambler
His Lost Lycan Luna Chapter 5
Blanchard St Denis Funeral Home Obituaries
<i>1883</i>'s Isabel May Opens Up About the <i>Yellowstone</i> Prequel
Mylaheychart Login
Songkick Detroit
Kent And Pelczar Obituaries
How Far Is Chattanooga From Here
Draconic Treatise On Mining
LeBron James comes out on fire, scores first 16 points for Cavaliers in Game 2 vs. Pacers
Lenscrafters Huebner Oaks
Finger Lakes Ny Craigslist
SXSW Film & TV Alumni Releases – July & August 2024
Caledonia - a simple love song to Scotland
Wemod Vampire Survivors
Doublelist Paducah Ky
Engineering Beauties Chapter 1
Lines Ac And Rs Can Best Be Described As
Urban Dictionary Fov
Yale College Confidential 2027
Vht Shortener
TJ Maxx‘s Top 12 Competitors: An Expert Analysis - Marketing Scoop
Gt7 Roadster Shop Rampage Engine Swap
Ryujinx Firmware 15
Allegheny Clinic Primary Care North
Kiddie Jungle Parma
Ff14 Sage Stat Priority
Appleton Post Crescent Today's Obituaries
Pensacola 311 Citizen Support | City of Pensacola, Florida Official Website
R&J Travel And Tours Calendar
The best Verizon phones for 2024
Empires And Puzzles Dark Chest
Felix Mallard Lpsg
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Stanley Steemer Johnson City Tn
Cranston Sewer Tax
2020 Can-Am DS 90 X Vs 2020 Honda TRX90X: By the Numbers
The Listings Project New York
Pro-Ject’s T2 Super Phono Turntable Is a Super Performer, and It’s a Super Bargain Too
The Attleboro Sun Chronicle Obituaries
Exploring the Digital Marketplace: A Guide to Craigslist Miami
4k Movie, Streaming, Blu-Ray Disc, and Home Theater Product Reviews & News
Rise Meadville Reviews
Rheumatoid Arthritis Statpearls
18443168434
Superecchll
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.