如何对二进制进行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
相关问题
jsbind--二进制包导入指导
758浏览 • 1回复 待解决
native层如何访问rawfile的二进制文件
1862浏览 • 1回复 待解决
如何直接加载二进制流的图片?
2641浏览 • 1回复 待解决
HarmonyOS gzip二进制流压缩和解压缩
622浏览 • 1回复 待解决
如何进行不同规格的AES解密
708浏览 • 1回复 待解决
HarmonyOS AES解密报错
524浏览 • 1回复 待解决
使用AES解密时,一直在报解密异常
117浏览 • 1回复 待解决
HarmonyOS AES解密与java无法通用
397浏览 • 1回复 待解决
如何Serviceabbility进行调试?
2911浏览 • 1回复 待解决
AES解密长字符串是否需要分段
488浏览 • 1回复 待解决