Search code examples
javascriptnode.jsencryption3destripledes

3des encryption in Node.JS returning invalid IV length


I'm quite new to Node and have run into an issue with the encryption object:

var des3_key = new Buffer("redacted", "base64"); // copied from key in chilk
var des3_iv = new Buffer("alsoredacted", "base64"); // copied from iv in chilk
var des3_encryption = crypto.createCipheriv("des3", des3_key, des3_iv);
// encode a string
var string_to_encode = "thisisatest";
var ciphered_string = des3_encryption.update(string_to_encode, "utf8", "base64");
console.log(string_to_encode+" "+ciphered_string);

Both in the Node console and when running on the server, line 6 causes an error node-crypto: Invalid IV length 32 instead of returning an encryption object, as expected.

The key and IV I've removed and their encryption types are copied from another file but for the sake of testing I have tried various strings and encryption types but still get the same error, though with different lengths in the error.

My knowledge of encryption is limited to what I've used previously and not much else unfortunately, and I'm having trouble finding troubleshooting resources for Node in this regard. Any help would be appreciated.

Edit: Experimenting with des and des3 yields the same result.


Solution

  • From OP's edit:

    SOLVED:

    Working code:

    var string_to_decode = "encrypted string";
    var des_key = new Buffer("key string", "base64");
    var des_iv = new Buffer(0);
    var des_decryption = Crypto.createDecipheriv("DES-EDE3", des_key, des_iv);
    var deciphered_string = des_decryption.update(string_to_decode, "base64", "utf8");
        console.log("["+string_to_decode+"] => ["+deciphered_string+"]");
    

    I found this by making a script to guess combinations of key and IV lengths, and encryption types and methods, and encoding types until it resulted in the correct string. It was a last resort but it worked.