cryptoFramework.createCipher,可否提供AES解密的例子?

cryptoFramework.createCipher,可否提供AES解密的例子?

HarmonyOS
2024-06-05 23:17:07
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
coolhead2000

参考代码:

public static async aesDecrypt(text: string,key:string) { 
  let globalResult = "" 
  try { 
    let cipherAlgName = 'AES256|ECB|NoPadding'; 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
 
    let symAlgName = 'AES256'; 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
 
    let base64Helper = new util.Base64Helper; 
    let dataUint8Array = base64Helper.decodeSync(key) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
 
    await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, null); 
    let plainText: cryptoFramework.DataBlob = { data: base64Helper.decodeSync(text) } 
    let result = await globalCipher.doFinal(plainText) 
    globalResult = Network.uint8ArrayToString(result.data); 
    console.log("解密后的明文==》"+globalResult) 
  }catch (err){ 
    console.log(err.message) 
  } 
} 
 
static uint8ArrayToString(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; 
    } 
  } 
  return out; 
}
分享
微博
QQ
微信
回复
2024-06-06 23:04:28
相关问题
需要提供napi反调ets例子
569浏览 • 1回复 待解决
如何进行不同规格AES解密
289浏览 • 1回复 待解决
AES解密长字符串是否需要分段
203浏览 • 1回复 待解决
解密HmacSha1 、HmacSha256、aes参考Demo
787浏览 • 1回复 待解决
Huks如何导入AES密钥?
340浏览 • 1回复 待解决
cryptoFramework.Md update 失败报错
657浏览 • 1回复 待解决
shell脚本中,可否执行sdkmgr命令?
5089浏览 • 1回复 待解决
是否有AESECB模式加密算法
251浏览 • 1回复 待解决
请问文中这个AESkey支持多少位?
310浏览 • 1回复 待解决
请问大家有没有ArkUI调用相机例子
3006浏览 • 1回复 待解决
多态样式可否导出给全局使用
690浏览 • 1回复 待解决
基于加解密算法框架规格问题
251浏览 • 1回复 待解决