David Garcia
David Garcia

Reputation: 2696

Decrypt - Encrypt crypto-js

I am trying to get crypto-js library to encrypt/decrypt a simple message, please see the following jsfiddle (http://jsfiddle.net/6gunq2nx/)

<script>
    var encrypted = CryptoJS.AES.encrypt("this is some test", "770A8A65DA156D24EE2A093277530142");
    var decrypted = CryptoJS.AES.decrypt(encrypted, "770A8A65DA156D24EE2A093277530142");
    alert(decrypted);
</script>

The problem is that, it is not decrypting the message properly, I have tried AES and DES but both do not work, what im I doing wrong? please see below screenshot

AES

Upvotes: 3

Views: 2600

Answers (2)

Umesh Sehta
Umesh Sehta

Reputation: 10659

try this :-

// Replace this with user input (only user should know the passphrase which can be used to decrypt the message)
var passphrase = '770A8A65DA156D24EE2A093277530142';

// Some content that we want to crypt
var content = 'this is some test';

// Use CryptoJS.AES to encrypt content using AES (Advanced Encryption Standard)
 var encryptedContent = CryptoJS.AES.encrypt(content, passphrase);

// Use CryptoJS.AES also to decrypt content
 var decryptedContent = CryptoJS.AES.decrypt(encryptedContent, passphrase).toString(CryptoJS.enc.Utf8);

alert(encryptedContent);
alert(decryptedContent);

Demo

Upvotes: 1

schnaader
schnaader

Reputation: 49729

It's almost correct. The string you get is a hexadecimal representation of your original string. Try to convert it like this:

var decrypted = CryptoJS.AES.decrypt(encrypted, "770A8A65DA156D24EE2A093277530142").toString(CryptoJS.enc.Utf8);

forked jsfiddle: http://jsfiddle.net/1qgzk9j8/

Upvotes: 4

Related Questions