AES Decryption in NodeJs [closed]

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.

  1. To generate the AES Key I used the following code:
    const key = crypto.randomBytes(32); // Output: aesEncryptionKeyaesEncryptionKey (example)

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

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

    let key = crypto.randomBytes(32);
    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()]);
    

But I am getting the following error

js error

  • The error you’re encountering is likely due to the fact that you’re trying to decrypt the data using the wrong key. The key you’re using to decrypt the data is a string, but the key you generated in step 1 is a Buffer.

    – 

  • 3

    Please read Why should I not upload images of code/data/errors when asking a question?

    – 

  • …Then I encoded that key and sent it to the server… Which encoding? Have you checked whether the server receives the key undamaged and decodes it correctly?

    – 

  • …where they used that key to encrypt data… How does the server encrypt the data? Have you checked whether the logic matches yours? Post the encryption code.

    – 

Leave a Comment