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

nodejs 3des decrypt didn't get expected result

我的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”

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

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

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

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