AES Encryption & Decryption using NodeJS (2024)

Cyber Security is very important irrespective of which domain you are in. In this article, I will show how to implement AES 256 encryption and decryption using NodeJS backend. With increase in cyber attacks around the world, it becomes irresistible for developers to use various encryption algorithms.

Please note we can’t decrypt data on client side. This will lead to exposure of secret key and application will become vulnerable.

Let’s start :)

We will set up NodeJS project first and then add dependancies.Create a folder called aes-using-node. Open command prompt from project folder and run below command.

npm init

This will create package.json in project folder. From same command prompt run

npm install

It will install dependancy in the project and after installation is done, you will see node_modules folder and package-lock.json. Create app.js file where all server code will be written.

Lets know about AES npm packages and various algorithm types.
Official documentation can be found on link below

Here we will be using AES 256 algorithm for encryption and decryption.There are two modes in AES algorithm : -

  1. EBC
  2. CBC

We will be implementing EBC mode for now.

Nothing fancy yet.

From root of project, run below command

npm i crypto-js — save

Create a file called crypto.js in utility folder and paste below code

‘use strict’;
const CryptoJS = require(‘crypto-js’);

module.exports = {
aesEncrypt: aesEncrypt,
aesDecrypt: aesDecrypt
};

function aesEncrypt(content) {
const parsedkey = CryptoJS.enc.Utf8.parse(secret_key);
const iv = CryptoJS.enc.Utf8.parse(your_secret_iv);
const encrypted = CryptoJS.AES.encrypt(content, parsedkey, { iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
return encrypted.toString();
};

function aesDecrypt(word) {
var keys = CryptoJS.enc.Utf8.parse(secret_key);
let base64 = CryptoJS.enc.Base64.parse(word);
let src = CryptoJS.enc.Base64.stringify(base64);
var decrypt = CryptoJS.AES.decrypt(src, keys, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
return decrypt.toString(CryptoJS.enc.Utf8);
};

AES Encryption & Decryption using NodeJS (2024)
Top Articles
Forensic Biology
What Is a BIP39?
Walgreens Boots Alliance, Inc. (WBA) Stock Price, News, Quote & History - Yahoo Finance
Sprinter Tyrone's Unblocked Games
Metallica - Blackened Lyrics Meaning
4-Hour Private ATV Riding Experience in Adirondacks 2024 on Cool Destinations
Booknet.com Contract Marriage 2
Ofw Pinoy Channel Su
Fusion
P2P4U Net Soccer
No Credit Check Apartments In West Palm Beach Fl
New Mexico Craigslist Cars And Trucks - By Owner
Beau John Maloney Houston Tx
Red Tomatoes Farmers Market Menu
Used Drum Kits Ebay
DoorDash, Inc. (DASH) Stock Price, Quote & News - Stock Analysis
Vermont Craigs List
Walgreens Alma School And Dynamite
Atdhe Net
Happy Life 365, Kelly Weekers | 9789021569444 | Boeken | bol
Stoney's Pizza & Gaming Parlor Danville Menu
Dark Entreaty Ffxiv
eugene bicycles - craigslist
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
Is Poke Healthy? Benefits, Risks, and Tips
Evil Dead Rise Ending Explained
Chelsea Hardie Leaked
UAE 2023 F&B Data Insights: Restaurant Population and Traffic Data
LG UN90 65" 4K Smart UHD TV - 65UN9000AUJ | LG CA
A Plus Nails Stewartville Mn
Gyeon Jahee
Craigslist Albany Ny Garage Sales
Indiana Wesleyan Transcripts
Mistress Elizabeth Nyc
Tds Wifi Outage
Timberwolves Point Guard History
2007 Peterbilt 387 Fuse Box Diagram
Seminary.churchofjesuschrist.org
Mugshots Journal Star
Clausen's Car Wash
Windshield Repair & Auto Glass Replacement in Texas| Safelite
Sallisaw Bin Store
Craigslist Minneapolis Com
2013 Honda Odyssey Serpentine Belt Diagram
Holzer Athena Portal
John Wick: Kapitel 4 (2023)
Zeeks Pizza Calories
Ssss Steakhouse Menu
The Missile Is Eepy Origin
Bumgarner Funeral Home Troy Nc Obituaries
Affidea ExpressCare - Affidea Ireland
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6254

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.