【问题标题】:Mapping OpenSSL command line AES encryption to NodeJS Crypto API equivalent将 OpenSSL 命令行 AES 加密映射到等效的 NodeJS Crypto API
【发布时间】:2013-02-18 02:09:24
【问题描述】:

在尝试理解 Node.js Crypto API 时,我一直在尝试将代码与等效的 OpenSSL enc 命令行(插入换行符以提高可读性)相匹配,该命令行通过 xxd 传输以进行十六进制显示。

$ 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 时,我得到以下输出:

b8f995c4eb9691ef726b81a03681c48e

这与我的预期输出不匹配(它甚至是长度的三分之一)。这是我的test-aes.js 文件:

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 时我哪里出错了?

【问题讨论】:

    标签: javascript node.js cryptography openssl aes


    【解决方案1】:

    来吧:

    var cipher = crypto.createCipheriv("aes256", key, iv);
    var crypted = cipher.update(testVector.plaintext, "utf8", "hex");
    crypted += cipher.final("hex");
    console.log(crypted);
    

    您的原始代码有两个问题

    1. createCipher 是错误的函数,你应该使用createCipheriv
    2. update 有返回值,需要跟踪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多