繁体   English   中英

将AES Java解密代码移植到Node.js

[英]Porting AES Java decipher code 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);

这是我使用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);

而且,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);

这两个版本都生成正确长度的相同输出(与输入的长度相同),但此输出与在同一输入缓冲区上运行的解码的Java版本生成的明文不同。 如何设置像上面配置的Java解码器一样的Node.js解码?

谢谢。

createDecipher实际上不像在Java中那样使用密钥。 它使用密码,该密码被输入基于密码的密钥导出函数(PBKDF)以导出密钥。 因此,使用不同的密钥并且无法检查密钥的正确性,您将获得随机的纯文本。

Node.js API命名不是很好,函数createDeciphercreateDecipheriv建议后者与前者相同,增加了一个IV。 相反, createDecipher会将整个键派生函数添加到组合中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM