如何对二进制进行AES解密?

对请求返回的ArrayBuffer数据进行AES解密没有成功。

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

代码可参考如下案例:

export function test3(data:ArrayBuffer, key:string,iv:string){ 
  let symAlgName = 'AES128'; 
  let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
  console.info(`symKeyGenerator algName: ${symKeyGenerator.algName}`); 
  // Generate CBC parameter specifications. 
  let ivParam: cryptoFramework.IvParamsSpec = { 
    algName: 'IvParamsSpec', 
    iv: { 
      //如果项目需要就将空字符替换 
      data: stringToUint8Array(iv) 
    } 
  } 
  // Create a Cipher instance. 
  let cipherAlgName = 'AES128|CBC|PKCS7'; 
  let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
  console.info(`cipher algName: ${globalCipher.algName}`); 
  // Use the key generator to randomly generate a 128-bit symmetric key. 
  let globalCipherText: cryptoFramework.DataBlob; 
  let globalKey: cryptoFramework.SymKey; 
  let promiseSymKey = symKeyGenerator.convertKey({data:stringToUint8Array(key)}); 
  promiseSymKey.then(key => { 
    // Initialize the Cipher instance and start encryption. 
    globalKey = key; 
    let mode = cryptoFramework.CryptoMode.ENCRYPT_MODE; 
    return globalCipher.init(mode, globalKey, ivParam); 
  }) 
    .then(() => { 
      let finalData = new Uint8Array(); 
      return globalCipher.doFinal({data:a});//a是传入的数据数组 
    }) 
    .then(finalOutput => { 
      uint8ArrayToString(finalOutput.data) 
      if (finalOutput != null) { // Check whether the result is null before using finalOutput.data. 
        console.info('CBC finalOutput is null'); 
      } 
    }) 
    .catch((error: BusinessError) => { 
      console.error(`catch error, ${error.code}, ${error.message}`); 
    }) 
}
分享
微博
QQ
微信
回复
2024-11-08 16:57:23
相关问题
HarmonyOS 二进制转16进制
74浏览 • 1回复 待解决
如何直接加载二进制流的图片?
2838浏览 • 1回复 待解决
HarmonyOS 十进制二进制方法
94浏览 • 1回复 待解决
HarmonyOS gzip二进制流压缩和解压缩
855浏览 • 1回复 待解决
jsbind--二进制包导入指导
851浏览 • 1回复 待解决
native层如何访问rawfile的二进制文件
1955浏览 • 1回复 待解决
如何进行不同规格的AES解密
858浏览 • 1回复 待解决