javascriptnode.jscryptojsrc4-ciphernode-crypto

NodeJS Crypto with RC4 yields blank


I have a php function that generates an RC4 encrypted string. I would like to decode that string using Node - ideally using the built in Crypto module. But I am unable to do so - I just get a blank string.

The PHP code is here http://code.google.com/p/rc4crypt/

My JS code is

crypto = require('crypto');
decipher = crypto.createDecipher("rc4", "MY SECRET KEY");
text = "HELLO";
decrypted = decipher.update(text, "utf8", "hex");
decrypted += decipher.final("hex");
console.log(decrypted);

I don't get any output. I have checked that my OpenSSL implementation has RC4 using openssl list-message-digest-algorithms

I am on OSX 10.8, latest node.

I am open to using another module to decrypt - I tried the cryptojs module but did not figure out how to make it work - gave me errors when I tried RC4.

Thanks


Solution

  • Figured it out

    First one must use crypto.createDecipheriv otherwise the key is - I believe - md5 hashed instead of used raw.

    Secondly the input encoding mut be set to binary.

    Third - in my case I was dealing with POST data instead of a hardcoded string and I had to urldecode it - decodeURIComponent() jsut choked - but unescape() with removal of + signs did the trick ex:

    var text = unescape((response.post.myvar + '').replace(/\+/g, '%20'))
    
    var crypto = require('crypto');
    decipher = crypto.createDecipheriv("rc4", key, '');    
    decrypted = decipher.update(text, "binary", "utf8");
    decrypted += decipher.final("utf8");
    console.log(decrypted);