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
相关问题
HarmonyOS 是否有rsa加解密例子
22浏览 • 1回复 待解决
需要提供napi反调ets例子
925浏览 • 1回复 待解决
HarmonyOS AES解密报错
706浏览 • 1回复 待解决
HarmonyOS RAS加密,AES加密和解密
48浏览 • 1回复 待解决
如何进行不同规格AES解密
843浏览 • 1回复 待解决
使用AES解密时,一直在报解密异常
330浏览 • 1回复 待解决
HarmonyOS AES解密与java无法通用
506浏览 • 1回复 待解决
如何对二进制进行AES解密
239浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
32浏览 • 1回复 待解决
AES解密长字符串是否需要分段
595浏览 • 1回复 待解决
HarmonyOS 请提供RAS加解密文档
391浏览 • 1回复 待解决
解密HmacSha1 、HmacSha256、aes参考Demo
2206浏览 • 1回复 待解决