HarmonyOS crypto-js 解密中乱码问题-鸿蒙开发者社区-51CTO.COM

HarmonyOS crypto-js 解密中乱码问题

HarmonyOS里用的第三方aes加解密库 crypto-js 在解密时候中文乱码,看了github和HarmonyOS开发的论坛上有多人提出了这个Issue, 应该是这个库的现存的一个bug, 这边也尝试了其他人的解决方案,都无法解决乱码问题,相关链接:

https://developer.huawei.com/consumer/cn/forum/topic/0201138040681270128

https://gitee.com/openharmony-sig/crypto-js/issues/I983QP?from=project-issue

请问下,HarmonyOS这边有没有这种情况的解决方案?

HarmonyOS
2024-09-13 10:11:15
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

是否有意愿使用系统的cryptoFramework进行加解密。参考资料:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-aes-sym-encrypt-decrypt-gcm-V5

参考demo:

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
class Stu { 
  name:string = '小明' 
  age: number=18 
} 
function genGcmParamsSpec() { 
  let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes 
  let dataIv = new Uint8Array(arr); 
  let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 
  arr = [0, 0, 0, 0, 0, 0, 0, 0]; // 8 bytes 
  let dataAad = new Uint8Array(arr); 
  let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; 
  arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes 
  let dataTag = new Uint8Array(arr); 
  let tagBlob: cryptoFramework.DataBlob = { 
    data: dataTag 
  }; 
  // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 
  let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { 
    iv: ivBlob, 
    aad: aadBlob, 
    authTag: tagBlob, 
    algName: "GcmParamsSpec" 
  }; 
  return gcmParamsSpec; 
} 
let gcmParams = genGcmParamsSpec(); 
// 加密消息 
async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 
  let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); 
  let encryptUpdate = await cipher.update(plainText); 
  // gcm模式加密doFinal时传入空,获得tag数据,并更新至gcmParams对象中。 
  gcmParams.authTag = await cipher.doFinal(null); 
  return encryptUpdate; 
} 
// 解密消息 
async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 
  let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); 
  let decryptUpdate = await decoder.update(cipherText); 
  // gcm模式解密doFinal时传入空,验证init时传入的tag数据,如果验证失败会抛出异常。 
  let decryptData = await decoder.doFinal(null); 
  if (decryptData == null) { 
    console.info('GCM decrypt success, decryptData is null'); 
  } 
  return decryptUpdate; 
} 
async function genSymKeyByData(symKeyData: Uint8Array) { 
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 
  let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 
  let symKey = await aesGenerator.convertKey(symKeyBlob); 
  console.info('convertKey success'); 
  return symKey; 
} 
async function main() { 
  let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); 
  let symKey = await genSymKeyByData(keyData); 
  let message = JSON.stringify(new Stu()) 
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 
  let encryptText = await encryptMessagePromise(symKey, plainText); 
  let decryptText = await decryptMessagePromise(symKey, encryptText); 
  if (plainText.data.toString() === decryptText.data.toString()) { 
    console.info('decrypt ok'); 
    console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 
  } else { 
    console.error('decrypt failed'); 
  } 
} 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(async () => { 
            await main() 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

编码解密得配套,都使用这个库的加解密。没有出现中文乱码。建议加密也是用这个库。参考demo:

import { CryptoJS } from '@ohos/crypto-js'; 
class Stu { 
  name:string = '小明' 
  age: number=18 
} 
function aesDecrypt(word: string) { 
  let secret_key: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse("07E44219AED9CAC162A6D81xxxx26AD8957EB40C9DFF266C375840359") 
  let iv: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse("0A010B05040F0xxx06080C0D5B") 
  let decrypt  = CryptoJS.AES.decrypt(word, secret_key, { 
    iv: iv, 
    mode: CryptoJS.mode.CBC, 
    padding: CryptoJS.pad.Pkcs7 
  }) 
  return decrypt.toString(CryptoJS.enc.Utf8) 
} 
function aesDecrypt1() { 
  let secret_key: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse("07E44219AED9CAC162A6D818872B71433826AD8957EB40C9DFF266C375840359") 
  let iv: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse("0A010B05040F070917030106080C0D5B") 
 
  let encrypted  = CryptoJS.AES.encrypt(JSON.stringify(new Stu()), secret_key, { 
    iv: iv, 
    mode: CryptoJS.mode.CBC, 
    padding: CryptoJS.pad.Pkcs7 
  }) 
  let decrypt  = CryptoJS.AES.decrypt(encrypted, secret_key, { 
    iv: iv, 
    mode: CryptoJS.mode.CBC, 
    padding: CryptoJS.pad.Pkcs7 
  }) 
  return decrypt.toString(CryptoJS.enc.Utf8) 
} 
@Entry 
@Component 
struct Page { 
  @State message: string = 'Hello World'; 
  private work:string = `xyB/WodmF0QwquruFxxxXY=` 
  build() { 
    Row() { 
      Column() { 
 
        Text('aesDecrypt').fontSize(20) 
          .margin(20) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            let str =   aesDecrypt(this.work) 
            console.info("str: "+  str) 
          }) 
        Text('aesDecrypt1').fontSize(20) 
          .margin(20) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            let str1 =   aesDecrypt1() 
            console.info("str1: "+  str1) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-09-13 18:04:27
相关问题
HarmonyOS crypto-js 性能存在问题
328浏览 • 1回复 待解决
鸿蒙ets项目如何引用crypto-js?
4157浏览 • 1回复 待解决
加密算法(crypto-js)Arkts转化代码
1010浏览 • 1回复 待解决
HarmonyOS 中文base64加密解密乱码
472浏览 • 1回复 待解决
HarmonyOS RSA解密问题
391浏览 • 1回复 待解决
如何解决文件的中文乱码问题
2653浏览 • 3回复 待解决
HarmonyOS 文档没有查找到DES加解密
334浏览 • 1回复 待解决
HarmonyOS RSA分段加密乱码
499浏览 • 1回复 待解决
HarmonyOS Sm2和DES加解密问题
360浏览 • 1回复 待解决
HarmonyOS 字节数组转中文出现乱码
83浏览 • 1回复 待解决
解密问题的定位指导
330浏览 • 1回复 待解决
HarmonyOS web iframe 注入.js 问题
394浏览 • 1回复 待解决
js class类名动态绑定的问题
6365浏览 • 1回复 待解决
HarmonyOS js调用webview的方法
359浏览 • 1回复 待解决
HarmonyOS arraybuffer转base64乱码
62浏览 • 1回复 待解决