3

I have a simple nodeJS program to encrypt plaintext:

var crypto = require("crypto");

var compatEnc = crypto.createCipher("aes-256-cbc", "password");
compatCrypted = compatEnc.update("Message", "utf8", "hex");
compatCrypted += compatEnc.final("hex");
console.log(compatCrypted);
// 0293cf0bdf5323cff809ba406ffc8283

I try to decrypt 0293cf0bdf5323cff809ba406ffc8283 on the browser

<!doctype html>
<html>
  <body>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js"></script>

    <script>

    var nosalt = CryptoJS.lib.WordArray.random(0);

    // { salt : null } will generate random salt
    var enc = CryptoJS.AES.decrypt("0293cf0bdf5323cff809ba406ffc8283", "password",
    { salt: nosalt });

    console.log(CryptoJS.enc.Utf8.stringify(enc));

    </script>
  </body>
</html>

The output is blank. Can you tell me what is wrong with my decryption code? Thanks.

5
  • 4
    You know if you hardcode the key into the JS, this encryption is going to be useless, right? Commented Jul 29, 2013 at 20:37
  • I am just doing a test. Commented Jul 29, 2013 at 20:46
  • Did you first try decrypting in node.js? I recommend you try that, and then move to the browser. Commented Jul 29, 2013 at 20:57
  • the decrypt with nodeJS is easy:var crypto = require("crypto"); var compatEnc = crypto.createDecipher("aes-256-cbc", "password"); compatCrypted = compatEnc.update("0293cf0bdf5323cff809ba406ffc8283", "hex", "utf8"); compatCrypted += compatEnc.final("utf8"); console.log(compatCrypted); // Message Commented Jul 29, 2013 at 21:04
  • did you look at the npm cryptojs module? npm.im/cryptojs Commented Jul 29, 2013 at 22:04

1 Answer 1

2

CryptoJS doesn't know that your encrypted text is hex-encoded.

Convert it to a WordArray first using CryptoJS.enc.Hex.parse(...);.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.