#鸿蒙学习大百科#中文加解密的乱码如何解决?

中文加解密的乱码如何解决?

HarmonyOS
2024-10-30 10:06:07
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
耗子煨汁r

​对于 CryptoJS 鸿蒙版 aes 解密中文乱码,解决方案是:

decrypt(word: ESObject, key: ESObject, iv: ESObject): ESObject {
// let encryptedHexStr: ESObject = CryptoJS.enc.Hex.parse(word);
// const srcs: ESObject = CryptoJS.enc.Base64.stringify(encryptedHexStr);
const decrypt: ESObject = CryptoJS.AES.decrypt(word, key, {
iv: iv,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
const decryptedStr: ESObject = decrypt.toString(CryptoJS.enc.Ut
}

​对于使用 SHA256 时原始字符串中有中文导致和后端算出的结果不一致的问题,参考代码如下:

function stringToUint8ArrayByte(str: string) {
return new Uint8Array(buffer.from(str,'utf-8').buffer);
}
// 字节流转成可理解的字符串
function uint8ArrayToStringByte(array:Uint8Array) {
// 将UTF-8编码转换成Unicode编码
let out: string = '';
let index: number = 0;
let len: number = array.length;
while (index < len) {
let character = array[index++];
switch(character >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
out += String.fromCharCode(character);
break;
case 12:
case 13:
out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F));
break;
case 14:
out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) | ((array[index++] & 0x3F) << 0));
break;
default:
break;
}
}
}
分享
微博
QQ
微信
回复
2024-10-30 15:21:18
相关问题
如何解决文件中文乱码问题
2702浏览 • 3回复 待解决