HarmonyOS 鸿蒙Next解密中文乱码
HarmonyOS 鸿蒙Next解密中文乱码
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
关于HarmonyOS 鸿蒙Next解密中文乱码的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
4 回复
问题描述
CryptoJS鸿蒙版aes解密,中文乱码
export function decryptByAESByKey(aesEncryptKey: string,plainText: string): string {
const key: string = CryptoJS.enc.Utf8.parse(aesEncryptKey);
const iv: string = CryptoJS.enc.Utf8.parse(’’);
let encryptedHexStr: [] = CryptoJS.enc.Hex.parse(plainText);
let srcs: string = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt: string = CryptoJS.AES.decrypt(srcs, key,
{ iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }
).toString(CryptoJS.enc.Utf8);
return decrypt.toString();
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
解决方案
请使用下面这种写法
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.Utf8);
return decryptedStr.toString();
}
AES加密的密钥长度需求16字节,不足在后面补"\0",如:f901c133de\0\0\0\0\0\0
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
感谢,刚好遇到这问题,有效解决。