croweman
croweman

Reputation: 35

Decrypt a node.js aes-256-cbc string in openssl

I have the following example code that generates an encrypted value in node.js using the aes-256-cbc algorithm, a fixed key, and an iv in node.js.

const crypto = require('crypto');

const iv = '0f9387b3f94bf06f';
const key = 'ZTk0YTU5YjNhMjk4NGI3NmIxNWExNzdi';

const encrypt = (value) => {
   const cipher = crypto.createCipheriv('AES-256-CBC', key, iv);
   let encrypted = cipher.update(value, 'utf8', 'hex');
   encrypted += cipher.final('hex');
   return encrypted;
};

console.log('Encrypted value: ', encrypt('HelloWorld'));

The encrypted value that is created by the above script, encrypted the string HelloWorld is the following hex value:

0c491f8c5256b9744550688fc54926e8

In bash and using openssl what script can I execute with the given encrypted value above to give me a decrypted value of HeloWorld?

I have spent lots of time struggling on this, using openssl, various options and accompanying commands but am struggling to generate the same output.

Any gurus out there who can help?

Upvotes: 1

Views: 1971

Answers (1)

oliv
oliv

Reputation: 13259

According to Node.js documentation, the function crypto.createCipher() doesn't use salt. You then need to explicitly say it to openssl.

Moreover the function you use doesn't expect IV. If you would require IV, then use crypto.createCipheriv().

So the openssl command is the following:

$ echo dd64232d380669d1a09378e1dc9de762 | xxd -ps -r | openssl enc -d -aes-256-cbc -k ZTk0YTU5YjNhMjk4NGI3NmIxNWExNzdi -nosalt
HelloWorld

Note that I'm using xxd to convert the string into bytes. You could use hexdump, od or other...


In response of the last OP's change, here the correct openssl command. Obviously if you specify explicitly the IV, you don't need the salt and need to the key in bytes format (option -K)

echo 0c491f8c5256b9744550688fc54926e8 | xxd -ps -r | openssl enc -d -aes-256-cbc -iv 30663933383762336639346266303666 -K 5a546b3059545535596a4e684d6a6b344e4749334e6d49784e5745784e7a6469

Upvotes: 3

Related Questions