HarmonyOS AES加解密报错17630001

export default class AESUtils {
  static async aesEncryptString(data: string): Promise<string> {
    let symKey = await genSymKeyByData(new Uint8Array(buffer.from('IaR90VTAdg2ZBRYT', 'utf-8').buffer));
    let plainText: cryptoFramework.DataBlob = {
      data: new Uint8Array(buffer.from(data, 'utf-8').buffer)
    };
    let encryptText = await encryptMessagePromise(symKey, plainText);
    return new util.Base64Helper().encodeToString(encryptText.data)
  }

  static async aesDecryptString(data: string): Promise<string> {
    let symKey = await genSymKeyByData(new Uint8Array(buffer.from('IaR90VTAdg2ZBRYT', 'utf-8').buffer))
    let plainText: cryptoFramework.DataBlob = {
      data: new Uint8Array(buffer.from(data, 'utf-8').buffer)
    };
    let decryptText = await decryptMessagePromise(symKey, plainText)
    return new util.Base64Helper().encodeToString(decryptText.data)

  }
}

async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('AES128|CBC|PKCS7');
  let iv = genIvParamsSpec();
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv);
  let cipherData = await cipher.doFinal(plainText);
  return cipherData;
}

// 解密消息
async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
  let decoder = cryptoFramework.createCipher('AES128|CBC|PKCS7');
  let iv = genIvParamsSpec();
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv);
  let decryptData = await decoder.doFinal(cipherText);
  return decryptData;
}

function genIvParamsSpec() {
  let ivBlob: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from('A-16-Byte-String', 'utf-8').buffer) };
  let ivParamsSpec: cryptoFramework.IvParamsSpec = {
    algName: "IvParamsSpec",
    iv: ivBlob
  };
  return ivParamsSpec;
}


async function genSymKeyByData(symKeyData: Uint8Array) {
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
  let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
  let symKey = await aesGenerator.convertKey(symKeyBlob);
  return symKey
}

这套代码AES加密没问题,但是解密的时候会出现17630001这个code

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

这个加密返回的时候使用了base64Utils的encodeToString函数将原本的返回值加密成了base64字符串,然后就直接对加密后的base64字符串进行了解密操作,需要先将加密后的base64字符串解密为uint8Array才行,可参考以下demo修改代码后再次进行尝试:

static async aesDecryptString(data: string,uint8:Uint8Array): Promise<string> {//解密
  let  str =''
  let uint8ArrayKey = new util.Base64Helper().decodeSync(data)//因为之前用base64对其进行了加密,所以如果要使用的话,先要把字符串进行解密还原为uint8Array
  let symKey = await genSymKeyByData(new Uint8Array(buffer.from('IaR90VTAdg2ZBRYT', 'utf-8').buffer))
  let plainText: cryptoFramework.DataBlob = {
    data: uint8ArrayKey
  };
  let decryptText = await decryptMessagePromise(symKey, plainText)
  return new util.Base64Helper().encodeToString(decryptText.data)

}

encodeToString参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-util-V5#encodetostring9

分享
微博
QQ
微信
回复
6天前
相关问题
HarmonyOS AES解密报错
745浏览 • 1回复 待解决
HarmonyOS AES加解密咨询
141浏览 • 1回复 待解决
HarmonyOS AES加解密问题
118浏览 • 1回复 待解决
HarmonyOS AES-CBC加解密
90浏览 • 1回复 待解决
HarmonyOS AES128|CBC|NoPadding 加密报错
108浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
315浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
578浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
115浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
58浏览 • 1回复 待解决
如何进行不同规格的AES加解密
940浏览 • 1回复 待解决
HarmonyOS AES加解密过程实在是调不通
90浏览 • 1回复 待解决
HarmonyOS 加解密方式 AES/ECB/PKCS5Padding
92浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
644浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
2267浏览 • 1回复 待解决
HarmonyOS rsa加密报错
101浏览 • 1回复 待解决
HarmonyOS 加解密问题
119浏览 • 1回复 待解决