Porting AES Java Decryption Code to Node.js

I have the following Java code that I would like to pass to Node.js:

// Java

byte[] rawKey = "deadbeefdeadbeef".getBytes("us-ascii");
SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
Cipher cip = Cipher.getInstance("AES/ECB/NoPadding");
cip.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] plaintext = cip.doFinal(ciphertext, 0, ciphertext.length);

Here is my attempt with Node.js using streams

// JS, using the crypto streams API

var decipher = crypto.createDecipher('aes-128-ecb', 'deadbeefdeadbeef');
decipher.setAutoPadding(false);
decipher.pipe(concat(function(plaintext) { console.log(plaintext); });
decipher.end(ciphertext);

And also Node.js attempt to use the older .update()and .final()API:

// JS, using the `.update()` and `.final()` API

var decipher = crypto.createDecipher('aes-128-ecb', 'deadbeefdeadbeef');
decipher.setAutoPadding(false);
var pieces = [];
pieces.push(new Buffer(decipher.update(ciphertext)));
pieces.push(new Buffer(decipher.final()));
var plaintext = Buffer.concat(pieces);

Both of these versions produce the same output of the correct length (the same length as the input), but this output is not the same plaintext that is created by the version of Java decryption that runs in the same input buffer. How can I configure Node.js decryption, such as decrypted Java decryption?

Thanks.

+4
source share
1 answer

createDecipher , Java. , (PBKDF) . , .

API Node.js , createDecipher createDecipheriv , , , IV. createDecipher .

+4

Source: https://habr.com/ru/post/1525996/


All Articles