解密 - 加密crypto-js

时间:2014-08-28 12:51:20

标签: javascript algorithm encryption aes des

我正在尝试使用crypto-js库来加密/解密一条简单的消息,请参阅以下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>

问题是,它没有正确解密邮件,我尝试过AES和DES,但两者都不起作用,我做错了什么?请看下面的截图

AES

2 个答案:

答案 0 :(得分:3)

这几乎是正确的。您获得的字符串是原始字符串的十六进制表示形式。尝试将其转换为:

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

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

答案 1 :(得分:1)

试试这个: -

// 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