HarmonyOS [MSI] GCM 模式加密,cipher.update时报错

根据以下代码:

const cipher = cryptoFramework.createCipher("AES128|GCM|PKCS7");
const symKey = await genSymKeyByData(key);
const ivBlob: cryptoFramework.DataBlob = { data: new Uint8Array(Array(12).fill(0)) };
const aadBlob: cryptoFramework.DataBlob = { data: new Uint8Array(Array(8).fill(0)) };
const tagBlob: cryptoFramework.DataBlob = { data: new Uint8Array(Array(16).fill(0)) };
let gcmParamsSpec: cryptoFramework.GcmParamsSpec = {
  iv: ivBlob,
  aad: aadBlob,
  // 此处先写成空,下方会重制
  authTag: tagBlob,
  algName: "GcmParamsSpec",
};
const plainTextBlob: cryptoFramework.DataBlob = { data: plainText };
await cipher.init(
  cryptoFramework.CryptoMode.ENCRYPT_MODE,
  symKey,
  gcmParamsSpec
);
try{
  let encryptUpdate = await cipher.update(plainTextBlob);
}catch(e){
  console.log(e);
}
// gcm模式加密doFinal时传入空,获得tag数据,并更新至gcmParams对象中。
gcmParamsSpec.authTag = await cipher.doFinal(null);

在cipher.update报错,build context for update fail!

传入的plainText 为音频文件的内容

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

参考demo:

export async function aesGcmEncrypt(text: string, key: string): Promise<string> {
  let globalResult = ""
  try {
    let cipherAlgName = 'AES128|GCM|PKCS7';
    let symAlgName = 'AES128';
    let globalCipher = cryptoFramework.createCipher(cipherAlgName);
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName);
    let dataUint8Array = stringToUint8Array(key)
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array }
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob)

    gcmParamsSpec.iv = { data: stringToUint8Array("asdfghjklzxcvbnm") } //16位

    await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, gcmParamsSpec);
    let result = await globalCipher.update({ data: stringToUint8Array(text) })
    gcmParamsSpec.authTag = await globalCipher.doFinal({ data: stringToUint8Array(text) })
    globalResult = uint8ArrayToHexStr(result.data)
    console.log("globalResult => HEX" + globalResult)
  } catch (err) {
    console.log(err.message)
  }
  return globalResult;
}
分享
微博
QQ
微信
回复
1天前
相关问题
AES gcm模式加密有对应的库吗?
243浏览 • 1回复 待解决
cryptoFramework.Md update 失败报错
1830浏览 • 1回复 待解决
HarmonyOS 应用发布时报错
16浏览 • 1回复 待解决
HarmonyOS 运行HmosWorld时报错
240浏览 • 1回复 待解决
HarmonyOS 启动rn项目时报错
106浏览 • 1回复 待解决
HarmonyOS 加载激励视频时报错
328浏览 • 1回复 待解决
HarmonyOS 调用相册函数时报错
42浏览 • 1回复 待解决
RSA NoPadding模式加密失败
1274浏览 • 1回复 待解决
拉起UIAbility时报错16000050
2337浏览 • 1回复 待解决
HarmonyOS 使用flutter创建packages时报错
41浏览 • 1回复 待解决
HarmonyOS 获取推送token时报错1000900010
261浏览 • 1回复 待解决
HarmonyOS asset运行add方法时报错
361浏览 • 1回复 待解决
HarmonyOS rsa加密报错
45浏览 • 1回复 待解决
OLED 屏幕输出编译时报错问题
10773浏览 • 2回复 待解决
使用featureAbility.getContext()时报错如下
758浏览 • 1回复 待解决
使用ohpm发布tgz文件时报错
824浏览 • 1回复 待解决
hmscore接入安装HarmonyOS的npm包时报错
901浏览 • 1回复 待解决