3des加密在Node.JS中返回无效的IV长度

3des encryption in Node.JS returning invalid IV length

本文关键字:无效 IV 长度 返回 加密 Node JS 3des      更新时间:2023-09-26

我是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得到相同的结果

From OP's edit:

解决:工作代码:

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长度的组合,加密类型和方法,以及编码类型,直到产生正确的字符串。