DEV Community

Ridwaan
Ridwaan

Posted on

AES Decryption in NodeJs

I am working on a project where I need to implement a program that generates a 32-byte AES key (symmetric key), sends that key to a server, and then uses the same key to decrypt data received from the server. However, I am facing challenges in implementing this process effectively.

I am using the crypto module in NodeJs.

To generate the AES Key I used the following code:
`


const key = crypto.randomBytes(32);
// Output: aesEncryptionKeyaesEncryptionKey (example)

`

Then I encoded that key and sent it to the server where they used that key to encrypt data

Upon receiving this encrypted data I need to use the key that I generated in step 1 to decrypt the data.

let key = 'aesEncryptionKeyaesEncryptionKey';
let encrypted_text = 'wHCS+Op6ZDc2fy2xwezssW/ThsVq7r2bozo7zze5w5r8d5vtTjlwmZXZWb/d2H7z';

const decipher = crypto.createDecipheriv('aes-256-ecb', Buffer.from(key, 'utf8'), Buffer.alloc(0));
let decryptedKey = decipher.update(encrypted_text, 'base64', 'utf8');
decipher.setAutoPadding(true);
let decryptedKeybuffer = Buffer.concat([decryptedKey, decipher.final()]);
Enter fullscreen mode Exit fullscreen mode

But I am getting the following error

Image description
js error

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay