HarmonyOS 鸿蒙Next AES加解密报错

发布于 1周前 作者 eggper 来自 鸿蒙OS

HarmonyOS 鸿蒙Next AES加解密报错
<markdown _ngcontent-xce-c149="" class="markdownPreContainer">

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 } <button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>

上面的代码AES加密没问题,但是解密的时候会报错17630001,参考了这个文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-aes-sym-encrypt-decrypt-cbc-V5

没定位出问题,有大佬能帮忙分析下吗?

</markdown>

更多关于HarmonyOS 鸿蒙Next AES加解密报错的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS 鸿蒙Next AES加解密报错的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对HarmonyOS 鸿蒙Next AES加解密报错的问题,可能的原因包括:

  1. 密钥或IV不匹配:确保加密和解密时使用的密钥及初始化向量(IV)完全相同。任何不匹配都可能导致解密失败。
  2. 填充模式不一致:检查加密和解密时使用的填充模式是否一致。HarmonyOS的cryptoFramework可能默认使用PKCS#7或其他填充模式,需确保两端设置相同。
  3. 数据格式问题:确认加密数据的格式在传输过程中未被更改,且符合解密端的期望。如果数据在传输前进行了Base64编码,解密前需先进行解码。
  4. API使用错误:检查解密API的使用是否正确。例如,在调用解密函数时,应确保传入的是加密后输出的数据,而非加密函数本身。

请仔细排查上述可能的原因,并进行相应的调整。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部