Search code examples
node.jsencryption3des

nodejs 3des decrypt didn't get expected result


My nodejs 3DES decryption didn't get expected result. Is there anything wrong with my code?

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));

result is '90d49e1085d5251f87dbcd6292570ba30e8a6e0f84d8331c97a579ef0ba6ac867342f2f6'

however expected result should be '466F0198321458864627662057083A457601185220050000'


Solution

  • after some more testing found out need to add one more line of code before decrypt :

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

    complete code as below :

    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));
    

    result is

    decrypted 466f0198321458864627662057083a457601185220050000