Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrypt in Crypto-JS gives numeric hexadecimal output instead of original plaintext string

I put together a simple test using the example from the Crypto-JS source site at Google code:

In page header:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>

In a Javascript function:

var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");

alert('encrypted: '+encrypted+'  decrypted: '+decrypted);

but the output is:

encrypted: U2FsdGVkX19hsNqFBS5xcUoVBCu/hPHepEwZchqnUVU=
decrypted: 4d657373616765

image of decrypted output

What am I missing?

like image 651
Roy Hinkley Avatar asked Oct 08 '14 14:10

Roy Hinkley


People also ask

What encryption does CryptoJS use?

CryptoJS supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key. DES is a previously dominant algorithm for encryption, and was published as an official Federal Information Processing Standard (FIPS).

What is CryptoJS HmacSHA256?

HmacSHA256(CryptoJS. enc. Hex. parse(mess), key)) generates an HMAC using the SHA256 digest. Thereby the message is hex decoded and the key UTF8 encoded.

What is CryptoJS in JavaScript?

CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.


1 Answers

decrypted.toString(CryptoJS.enc.Utf8) // "Message"

See https://code.google.com/p/crypto-js/#The_Hasher_Output

The hash you get back isn't a string yet. It's a WordArray object. When you use a WordArray object in a string context, it's automatically converted to a hex string.

You can convert a WordArray object to other formats by explicitly calling the toString method and passing an encoder.

like image 113
Kyle Avatar answered Oct 05 '22 23:10

Kyle