Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node - Generate AES-CBC key and iv

I am trying to get nodejs to run the equivalent of

openssl enc -aes-192-cbc -k secret -P -md sha1

As described here: https://www.ibm.com/support/knowledgecenter/en/SSLVY3_9.7.0/com.ibm.einstall.doc/topics/t_einstall_GenerateAESkey.html

That is, generate a key/iv pair that will be compatible with other tools that expect AES CBC.

I could try to actually run openssl as a shell command, but I'm hoping there is a more node native way of doing so.

Could I generate 2 random hex of the correct length? One for iv and one for key?

like image 565
Nathan H Avatar asked Feb 01 '26 23:02

Nathan H


1 Answers

I am adding my own answer, using new NodeJS APIs available in v10:

let passphrase = "some passphrase"
let iv = crypto.randomBytes(16); // Initialization vector.
let salt = crypto.randomBytes(16);
let key = crypto.scryptSync(passphrase, salt, 16);

This generates a key and iv pair, compatible with AES-128-CBC

like image 128
Nathan H Avatar answered Feb 03 '26 14:02

Nathan H