Logo

dev-resources.site

for different kinds of informations.

AES Decryption in NodeJs

Published at
2/23/2024
Categories
javascript
node
aes
encryption
Author
ridwaan1
Categories
4 categories in total
javascript
open
node
open
aes
open
encryption
open
Author
8 person written this
ridwaan1
open
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

Featured ones: