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

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

HarmonyOS
13h前
浏览
收藏 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
微信
回复
7h前
相关问题
jsbind--二进制包导入指导
703浏览 • 1回复 待解决
如何直接加载二进制流的图片?
2583浏览 • 1回复 待解决
native层如何访问rawfile的二进制文件
1813浏览 • 1回复 待解决
HarmonyOS gzip二进制流压缩和解压缩
528浏览 • 1回复 待解决
如何进行不同规格的AES解密
647浏览 • 1回复 待解决
HarmonyOS AES解密报错
433浏览 • 1回复 待解决
HarmonyOS AES解密与java无法通用
335浏览 • 1回复 待解决
AES解密长字符串是否需要分段
444浏览 • 1回复 待解决
HarmonyOS 如何进行DES加解密
327浏览 • 1回复 待解决
如何Serviceabbility进行调试?
2879浏览 • 1回复 待解决