I've got the following Java code that I'd like to port to Node.js:
我有以下Java代码,我想移植到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's my attempt with Node.js, using streams
这是我使用Stream尝试使用Node.js
// 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 a Node.js attempt using the older .update()
and .final()
API:
而且,Node.js也尝试使用较旧的.update()和.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 produced by the Java version of the decipher operating on the same input buffer. How can I set up a Node.js decipher like the Java decipher configured above?
这两个版本都生成正确长度的相同输出(与输入的长度相同),但此输出与在相同输入缓冲区上运行的解码的Java版本生成的明文不同。如何设置像上面配置的Java解码器一样的Node.js解码?
Thank you.
谢谢。
1 个解决方案
#1
4
createDecipher
actually does not use a key as you do in Java. It uses a password, which is fed into a Password Based Key Derivation Function (PBKDF) to derive the key. Hence with a different key and no method of checking the correctness of the key, you will get random plain text.
createDecipher实际上不像在Java中那样使用密钥。它使用密码,该密码被输入基于密码的密钥导出函数(PBKDF)以导出密钥。因此,使用不同的密钥并且没有检查密钥正确性的方法,您将获得随机的纯文本。
The Node.js API is not very well named, the functions createDecipher
and createDecipheriv
suggest that the latter is just the same as the former, with the addition of an IV. Instead, createDecipher
adds an entire key derivation function to the mix.
Node.js API命名不是很好,函数createDecipher和createDecipheriv建议后者与前者相同,增加了一个IV。相反,createDecipher会将整个键派生函数添加到组合中。
#1
4
createDecipher
actually does not use a key as you do in Java. It uses a password, which is fed into a Password Based Key Derivation Function (PBKDF) to derive the key. Hence with a different key and no method of checking the correctness of the key, you will get random plain text.
createDecipher实际上不像在Java中那样使用密钥。它使用密码,该密码被输入基于密码的密钥导出函数(PBKDF)以导出密钥。因此,使用不同的密钥并且没有检查密钥正确性的方法,您将获得随机的纯文本。
The Node.js API is not very well named, the functions createDecipher
and createDecipheriv
suggest that the latter is just the same as the former, with the addition of an IV. Instead, createDecipher
adds an entire key derivation function to the mix.
Node.js API命名不是很好,函数createDecipher和createDecipheriv建议后者与前者相同,增加了一个IV。相反,createDecipher会将整个键派生函数添加到组合中。