将OpenSSL命令行AES加密映射到NodeJS Crypto API等效项 [英] Mapping OpenSSL command line AES encryption to NodeJS Crypto API equivalent
问题描述
为了了解 Node.js Crypto API,我一直在尝试匹配代码到等效 OpenSSL enc
命令行(为了可读性插入换行符),该命令行通过 xxd
十六进制显示。
In trying to understand the Node.js Crypto API, I have been trying to match code to the equivalent OpenSSL enc
command line (line breaks inserted for readability) which is piped through xxd
for hex display.
$ echo -n "The quick brown fox jumps over the lazy dog" | openssl enc -aes256 -e
-K "a891f95cc50bd872e8fcd96cf5030535e273c5210570b3dcfa7946873d167c57"
-iv "3bbdce68b2736ed96972d56865ad82a2" | xxd -p -c 64
582178570b7b74b000fd66316379835809874f985e0facadabb5b9c6b00593171165ae21c091f5237cea1a6fd939fd14
但是,当我运行 node test-aes.js
,我得到以下输出:
However, when I run node test-aes.js
, I get the following output:
b8f995c4eb9691ef726b81a03681c48e
这不符合我的预期输出(它甚至长度的三分之一)。这是我的 test-aes.js
文件:
Which does not match my expected output (it is even one third the length). Here is my test-aes.js
file:
var crypto = require("crypto");
var testVector = { plaintext : "The quick brown fox jumps over the lazy dog",
iv : "3bbdce68b2736ed96972d56865ad82a2",
key : "a891f95cc50bd872e8fcd96cf5030535e273c5210570b3dcfa7946873d167c57",
ciphertext : "582178570b7b74b000fd66316379835809874f985e0facadabb5b9c6b00593171165ae21c091f5237cea1a6fd939fd14"};
var key = new Buffer(testVector.key, "hex");
var iv = new Buffer(testVector.iv, "hex");
var cipher = crypto.createCipher("aes256", key, iv);
cipher.update(testVector.plaintext, "utf8");
var crypted = cipher.final("hex");
console.log(crypted);
问题:我在将OpenSSL参数映射到Node.js Crypto API?
Question: Where am I going wrong in mapping the OpenSSL parameters to the Node.js Crypto API?
推荐答案
这里ya go:
var cipher = crypto.createCipheriv("aes256", key, iv);
var crypted = cipher.update(testVector.plaintext, "utf8", "hex");
crypted += cipher.final("hex");
console.log(crypted);
您的原始程式码有两个问题
You have two issues with your original code
-
createCipher
是错误的函数,应该使用createCipheriv
。 -
更新
有一个返回值,您需要跟踪它。
createCipher
is the wrong function, you should be usingcreateCipheriv
.update
has a return value, you need to keep track of it.
这篇关于将OpenSSL命令行AES加密映射到NodeJS Crypto API等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!