Why I am getting the error Error: error:1C800064:Provider routines::bad decrypt?

948 Views Asked by At

Getting Error: error:1C800064:Provider routines::bad decrypt during decrypting code using crypto module in nodejs

This is the code for encrypting

const crypto = require('crypto');

// Generate a random 16-byte key for AES-128
const key = crypto.randomBytes(16).toString('hex');

// Initialization Vector (IV) - should be random for each encryption
const iv = Buffer.alloc(16).fill(0);

// Message to encrypt
const message = 'Hello, this is a secret message!';

// Create cipher object
const cipher = crypto.createCipheriv('aes-128-cbc', Buffer.from(key, 'hex'), iv);

// Encrypt the message
let encryptedMessage = cipher.update(message, 'utf-8', 'hex');
encryptedMessage += cipher.final('hex');

console.log('Encrypted Message:', encryptedMessage);
console.log('Key:', key);

I am giving the key and encrypted message given from the above module to the decryption module.

const crypto = require('crypto');

// Generate a random 16-byte key for AES-128
const key = '2e6c7f292306cd6518aff5ff99dba46e';

// Initialization Vector (IV) - should be random for each decryption
const iv = Buffer.alloc(16).fill(0);

// Message to decrypt
const message = '187145680cd07efad27ea86dbdbf7a0e7434dd5e753af1c427a147d621d7bb9980ac4ef633ec2504b8318e000757ed42';

// Create cipher object
const decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key, 'hex'), iv);

// decryptthe message
let decryptedMessage = decipher.update(message, 'utf-8', 'hex');
decryptedMessage += decipher.final('hex');

console.log('Decrypted Message:', decryptedMessage);
console.log('Key:', key);

I am expecting that the above message get decipher

0

There are 0 best solutions below