crypto-js aes无法解密加密的内容 [英] crypto-js aes can't decrypt what it encrypted

查看:2155
本文介绍了crypto-js aes无法解密加密的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用:

/ p>

 < script src =http://crypto-js.googlecode.com/svn/tags/3.1.2/build /rollups/aes.js\"> ;</script> 
< script>
var encrypted = CryptoJS.AES.encrypt(Message,Secret Passphrase);

var decryptpted = CryptoJS.AES.decrypt(encrypted,Secret Passphrase);
< / script>

但我的结果是:

 加密:U2FsdGVkX19whKq54yOQt3l1erbtEtn / M0qJjAH + E / E = 
解密:4d657373616765


$ b b

我的期望是它返回消息。

解决方案

所有 CryptoJS的返回类型< blockcipher> .decrypt )函数是一个 WordArray ,它是CryptoJS中二进制数据的本地表示。当你强制它被打印(自动调用 toString()),它包含的二进制数据将是十六进制编码。



如果你想要一个不同的编码,那么你需要指定它。以下是一些示例:

  console.log(decrypted.toString()); // Hex encoded 
console.log(decryptpted.toString(CryptoJS.enc.Hex)); // Hex encoded
console.log(CryptoJS.enc.Hex.stringify(decryptpted)); // Hex encoded

console.log(decryptpted.toString(CryptoJS.enc.Utf8)); // UTF-8 encoded
console.log(CryptoJS.enc.Utf8.stringify(decryptpted)); // UTF-8 encoded

console.log(decryptpted.toString(CryptoJS.enc.Base64)); // Base64 encoded
console.log(CryptoJS.enc.Base64.stringify(decryptpted)); // Base64 encoded


Hello I'm trying to decrypt a encrypted aes string with crypto-js but it seems like it doesn't work right.

I'm using:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
    var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");

    var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
</script>

But my results are:

encrypted: U2FsdGVkX19whKq54yOQt3l1erbtEtn/M0qJjAH+E/E=
decrypted: 4d657373616765

My expectation was the it returns back "Message". What am I missing?

解决方案

The return type of all CryptoJS.<blockcipher>.decrypt() functions is a WordArray which is the native representation of binary data in CryptoJS. When you force it to be printed (automatically calling toString()), the binary data that it contains will be Hex encoded.

If you want to have a different encoding, then you need to specify it. Here are some examples:

console.log(decrypted.toString()); // Hex encoded
console.log(decrypted.toString(CryptoJS.enc.Hex)); // Hex encoded
console.log(CryptoJS.enc.Hex.stringify(decrypted)); // Hex encoded

console.log(decrypted.toString(CryptoJS.enc.Utf8)); // UTF-8 encoded
console.log(CryptoJS.enc.Utf8.stringify(decrypted)); // UTF-8 encoded

console.log(decrypted.toString(CryptoJS.enc.Base64)); // Base64 encoded
console.log(CryptoJS.enc.Base64.stringify(decrypted)); // Base64 encoded

这篇关于crypto-js aes无法解密加密的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆