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例子
847浏览 • 1回复 待解决
HarmonyOS AES解密报错
505浏览 • 1回复 待解决
使用AES解密时,一直在报解密异常
105浏览 • 1回复 待解决
HarmonyOS AES解密与java无法通用
392浏览 • 1回复 待解决
如何进行不同规格AES解密
700浏览 • 1回复 待解决
AES解密长字符串是否需要分段
486浏览 • 1回复 待解决
如何对二进制进行AES解密
107浏览 • 1回复 待解决
HarmonyOS 请提供RAS加解密文档
312浏览 • 1回复 待解决
解密HmacSha1 、HmacSha256、aes参考Demo
1994浏览 • 1回复 待解决
可否提供样例代码?
344浏览 • 1回复 待解决