nodejs 3des decrypt没有得到预期的结果

时间:2017-11-16 18:01:46

标签: node.js encryption 3des

我的nodejs 3DES解密没有得到预期的结果。我的代码有什么问题吗?

let cryptojs = require('crypto-js')
var key = '551408C3D129EC64DA505CB48CD61D57551408C3D129EC64';
var ciphertext = 'C5F21F0F82038833851BA3092D613F085A60DB1E189E10F6';
var iv = cryptojs.enc.Hex.parse("0".repeat(16));

key = cryptojs.enc.Hex.parse(key);
var result = cryptojs.TripleDES.decrypt(ciphertext, key, {iv: iv, mode: cryptojs.mode.CBC, padding: cryptojs.pad.NoPadding});
console.log ('decrypted ' + result.toString(cryptojs.enc.Hex));

结果是'90d49e10​​85d5251f87dbcd6292570ba30e8a6e0f84d8331c97a579ef0ba6ac867342f2f6'

然而预期结果应为'466F0198321458864627662057083A457601185220050000'

2 个答案:

答案 0 :(得分:0)

你说你试图解密数据,但你加密了它:

var result = cryptojs.TripleDES.encrypt(ciphertext, key, {iv: iv, mode: cryptojs.mode.CBC, padding: cryptojs.pad.NoPadding});

答案 1 :(得分:0)

经过一些测试后发现需要在解密之前再添加一行代码:

ciphertext = cryptojs.format.Hex.parse(ciphertext);

完整代码如下:

let cryptojs = require('crypto-js')
var key = '551408C3D129EC64DA505CB48CD61D57551408C3D129EC64';
var ciphertext = 'C5F21F0F82038833851BA3092D613F085A60DB1E189E10F6';
var iv = cryptojs.enc.Hex.parse("0".repeat(16));

key = cryptojs.enc.Hex.parse(key);
ciphertext = cryptojs.format.Hex.parse(ciphertext);
var result = cryptojs.TripleDES.decrypt(ciphertext, key, {iv: iv, mode: cryptojs.mode.CBC, padding: cryptojs.pad.NoPadding});
console.log ('decrypted ' + result.toString(cryptojs.enc.Hex));

结果是

decrypted 466f0198321458864627662057083a457601185220050000