【问题标题】:3des encryption in Node.JS returning invalid IV lengthNode.JS 中的 3des 加密返回无效的 IV 长度
【发布时间】:2014-06-24 10:34:35
【问题描述】:

我对 Node 很陌生,遇到了加密对象的问题:

var des3_key = new Buffer("redacted", "base64"); // copied from key in chilk
var des3_iv = new Buffer("alsoredacted", "base64"); // copied from iv in chilk
var des3_encryption = crypto.createCipheriv("des3", des3_key, des3_iv);
// encode a string
var string_to_encode = "thisisatest";
var ciphered_string = des3_encryption.update(string_to_encode, "utf8", "base64");
console.log(string_to_encode+" "+ciphered_string);

无论是在 Node 控制台中还是在服务器上运行时,第 6 行都会导致错误 node-crypto: Invalid IV length 32,而不是像预期的那样返回加密对象。

我删除的密钥和 IV 以及它们的加密类型是从另一个文件复制的,但为了测试,我尝试了各种字符串和加密类型,但仍然得到相同的错误,尽管错误中的长度不同。

我对加密的了解仅限于我以前使用过的知识,不幸的是,我在查找 Node 的故障排除资源时遇到了麻烦。任何帮助将不胜感激。

编辑:使用 des 和 des3 进行试验会产生相同的结果。

【问题讨论】:

  • 可能是您的其他软件对密钥和 IV 接受的内容更为宽松。对于 3DES,密钥大小应为 128 位或 192 位(包括奇偶校验位),IV 应为 64 位。任何其他应该被拒绝。但例如 PHP 的 mcrypt 库只是简单地添加零或如果有太多位则切断位。
  • 我试过使用:var ivbuf = new Buffer(64) ivbuf.fill(0); ivbuf.write("已编辑", 0, 64, "base64"); var des3_encryption = crypto.createCipheriv("des3", keybuf, ivbuf);但是现在尝试创建密码时出现错误node-crypto : Invalid IV length 64。上面的填充可能是错误的,但长度应该是正确的。
  • 您不应将解决方案添加到您的问题中,而应将其作为答案发布。 SO 鼓励Answering your own question

标签: javascript node.js encryption 3des tripledes


【解决方案1】:

来自 OP 的编辑:

已解决:

工作代码:

var string_to_decode = "encrypted string";
var des_key = new Buffer("key string", "base64");
var des_iv = new Buffer(0);
var des_decryption = Crypto.createDecipheriv("DES-EDE3", des_key, des_iv);
var deciphered_string = des_decryption.update(string_to_decode, "base64", "utf8");
    console.log("["+string_to_decode+"] => ["+deciphered_string+"]");

我通过编写一个脚本来猜测密钥和 IV 长度、加密类型和方法以及编码类型的组合,直到得到正确的字符串。这是最后的手段,但它奏效了。

【讨论】:

  • 如果是 16 字节密钥,则使用 'des-ede' 算法。
猜你喜欢
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多