我的CryptoJS加密/解密不起作用 [英] My CryptoJS encryption/decryption is not working
问题描述
我有一个JSON数组数组,我尝试使用CryptoJS对其值进行加密,然后打印以供另一个文件使用,在该文件中,应使用用户指定的密码来解密这些值。
I have an array of JSON arrays whose values I am trying to encrypt with CryptoJS and then print for use in another file, where these values should be decrypted using a user-given passphrase.
但是我做错了事,解密URL时出现未捕获的错误:UTF-8数据格式错误。
But I am doing something wrong and I am getting "Uncaught Error: Malformed UTF-8 data" when decrypting the URL's.
encrypt.js:
encrypt.js:
var encrypted = CryptoJS.AES.encrypt(item[key], pass);
json[j] += encrypted.ciphertext.toString(CryptoJS.enc.Base64);
decrypt.js:
decrypt.js:
var decrypted = CryptoJS.AES.decrypt(item[key], pass);
html += '<a href="' + decrypted.toString(CryptoJS.enc.Utf8) + '" target="_blank" class="socialico ' + key + '">' + icons[key] + '</a>';
我关注了此示例...帮助,很高兴吗?
I followed this example... Help, pretty please?
推荐答案
该错误消息通常表示未正确解密数据,并且产生的纯文本字节不构成有效的UTF-8字符。
That error message usually means the data wasn't decrypted correctly, and the resulting plaintext bytes don't form valid UTF-8 characters.
要检查的几件事:
- 首先,请确保您使用相同的密码进行加密和解密。您可能希望保留正确密码的哈希值,以便可以在使用密码解密之前验证用户是否输入了正确密码。
- 第二,在加密之前,请确保值
item [key]
是字符串。 CryptoJS无法加密JSON对象。您必须先对其进行序列化。
- First, make sure you're using the same password for both encryption and decryption. You may want to keep a hash of the correct password so that you can verify if the user gave the correct password before you use it for decryption.
- Second, make sure that the value
item[key]
is a string before encrypting. CryptoJS can't encrypt JSON objects. You'll have to serialize it first.
这篇关于我的CryptoJS加密/解密不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!