使用来自crypto的createDecipheriv的无效密钥长度错误 [英] Invalid key length error using createDecipheriv from crypto
本文介绍了使用来自crypto的createDecipheriv的无效密钥长度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用CBC模式的三重DES编码解密数据。上面的代码是最后的尝试。创建解密程序时,我得到了以下错误:
I need to decrypt a data using de triple DES encoding with CBC mode. This code above is the unsussefuly last try. When the decipher will be created, i got the fallow error:
Error: Invalid key length
at Decipheriv.createCipherBase (internal/crypto/cipher.js:103:19)
at Decipheriv.createCipherWithIV (internal/crypto/cipher.js:121:20)
at new Decipheriv (internal/crypto/cipher.js:262:22)
at Object.createDecipheriv (crypto.js:127:10)
at Object.decrypt3DES (/home/didimo/projetos/node_anna_server/src/utils/exeCrypto.ts:132:25)
at /home/didimo/projetos/node_anna_server/src/controllers/annaController.ts:67:19
at step (/home/didimo/projetos/node_anna_server/src/controller`enter code here`s/annaController.ts:33:23)
at Object.next (/home/didimo/projetos/node_anna_server/src/controllers/annaController.ts:14:53)
at /home/didimo/projetos/node_anna_server/src/controllers/annaController.ts:8:71
at new Promise (<anonymous>)
有我的代码:
var data = "XAWHNurb7eQRxbA/vuC09Q==";
var iv = "O6VMaT2M9Pk=";
var key = "UqOKIBu82BMiz4hEh+TqJpobsO9DonWo";
var crypto = require("crypto");
var alg = "des-ede-cbc";
var keyBuffer64 = new Buffer(key, "base64");
var keyBufferUtf8 = new Buffer(keyBuffer64.toString("utf8"), "utf8");
var ivBuffer64 = new Buffer(iv, "base64");
var ivBufferUtf8 = new Buffer(ivBuffer64.toString("utf8"), "utf8");
var encryptedData = new Buffer(data, "base64");
var decipher = crypto.createDecipheriv(alg, keyBufferUtf8, ivBufferUtf8);
var decoded = decipher.update(encryptedData, "base64", "utf8");
decoded += decipher.final("utf8");
return decoded;
推荐答案
3天后,我通过反复试验发现了答案,缺少将iv和键值转换为十六进制。
After 3 days i discovered the answer by trial and error, was missing convert the iv and key values to hexadecimals. i hope this post helps somebody hehe.
var data = "aNwsjalhzHnZc0UvMjt8CA==";
var iv = "O6VMaT2M9Pk=";
var key = "JajN48do5d/p5EMk2AsxUiedQd7eusdK";
var ivBufferHex = Buffer.from(iv, "base64").toString("hex");
var keyBufferHex = Buffer.from(key, "base64").toString("hex");
var decipher = crypto.createDecipheriv(
"des-ede3-cbc",
Buffer.from(keyBufferHex, "hex"),
Buffer.from(ivBufferHex, "hex")
);
decipher.setAutoPadding(true);
var decryptedData = decipher.update(data, "base64", "utf8");
decryptedData += decipher.final("utf8");
return decryptedData;
这篇关于使用来自crypto的createDecipheriv的无效密钥长度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文