javascriptopensslcryptojs3des

cryptojs TDES convert to OPENSSL


I write this code in nodejs for decrypt 459508BB6B65C5A304D3EFB133038A14 with TripleDES:

C = require("crypto-js");
text = C.enc.Hex.parse("459508BB6B65C5A304D3EFB133038A14");
key = C.enc.Hex.parse("90033E3984CEF5A659C44BBB47299B4208374FB5DC495C96");
iv = C.enc.Hex.parse("E6B9AFA7A282A0CA");

var d = C.TripleDES.decrypt(
    {
        ciphertext: text
    }, 
        key,
    {
        iv: iv,
        mode: C.mode.CBC
    }
);
var r = d.toString(C.enc.Utf8);
console.log(r);

and now i want decrypt this with OpenSSL on command line. i try this command but not working:

echo -n 459508BB6B65C5A304D3EFB133038A14 | openssl enc -des3 -d -K 90033E3984CEF5A659C44BBB47299B4208374FB5DC495C96 -iv E6B9AFA7A282A0CA -nopad 

this command not work and return bad decrypted text


Solution

  • You forgot to hex decode the ciphertext itself, try:

    echo -n 459508BB6B65C5A304D3EFB133038A14 | xxd -p -r | openssl enc -des3 -d -K 90033E3984CEF5A659C44BBB47299B4208374FB5DC495C96 -iv E6B9AFA7A282A0CA
    

    Here xxd performs the hexadecimal decoding, using:

    -p | -ps | -postscript | -plain
         output in postscript continuous hexdump style. Also known as plain hexdump style.
    -r
         reverse operation: convert (or patch) hexdump into binary.
    

    Then it prints out 12 characters, including a space character at the end:

    1 0 6 4 0 3 
    

    Note that I also removed -nopad as it is not required anymore; crypto-js uses OpenSSL compatible padding (well, openssl compatible everything really).