解密 - 加密 crypto-js

3

我正在尝试使用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两种加密方式,但都没有成功。请看下面的截图,我做错了什么吗?

2
提示:字母“t”、“h”和“i”的数字等价物分别为116、104和105;在十六进制中,它们分别为74、68、69。 - Kevin
2个回答

4

几乎正确。你获得的字符串是原始字符串的十六进制表示。尝试像这样进行转换:

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

基于 jsfiddle 的分支版本: http://jsfiddle.net/1qgzk9j8/


太棒了,示例中没有提到转换为字符串:) 非常感谢! - David Garcia
1
这让我摆脱了调试的疯狂!谢谢! - A. Perez Cera

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);

演示


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接