简体   繁体   English

我的CryptoJS加密/解密无法正常工作

[英]My CryptoJS encryption/decryption is not working

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. 我有一个JSON数组的数组,其值我试图用CryptoJS加密,然后打印用于另一个文件,其中这些值应该使用用户给定的密码来解密。

But I am doing something wrong and I am getting "Uncaught Error: Malformed UTF-8 data" when decrypting the URL's. 但我做错了什么,我在解密URL时得到“未捕获的错误:格式错误的UTF-8数据”。

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? 我按照这个例子......帮忙,好吗?

That error message usually means the data wasn't decrypted correctly, and the resulting plaintext bytes don't form valid UTF-8 characters. 该错误消息通常意味着数据未正确解密,并且生成的明文字节不会形成有效的UTF-8字符。

A couple things to check: 要检查几件事:

  • 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. 其次,确保值item[key]是加密前的字符串。 CryptoJS can't encrypt JSON objects. CryptoJS无法加密JSON对象。 You'll have to serialize it first. 你必须先将它序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM