HarmonyOS AES目前加密成功,但是解密时出现crash
@Entry
@Component
struct Index {
aboutToAppear(): void {
AesUtils.encryptCBC('测试hello', 'newaircloud_vjow9Dej#JDj4[oIDF', (str: string) => {
this.str = str
})
}
@State str?: string = ''
@State str2?: string = ''
build() {
Column() {
Text('AES加密:' + this.str)
.height(100)
.backgroundColor(Color.Pink)
.onClick(() => {
AesUtils.encryptCBC('测试hello', 'newaircloud_vjow9Dej#JDj4[oIDF', (str: string) => {
this.str = str
})
})
Text('AES解密:' + this.str2)
.height(100)
.backgroundColor(Color.Pink)
.onClick(() => {
AesUtils.decryptCBC(this.str!, 'newaircloud_vjow9Dej#JDj4[oIDF', (str: string) => {
this.str2 = str
})
})
}
}
}
以下是工具类代码:
export class AesUtils {
static async encryptCBC(message: string, pk: string, callBack: Function) {
try {
let base64 = new util.Base64Helper();
let dataUnit = AesUtils.doMdBySync(pk)
let symKey = await AesUtils.genSymKeyByData(dataUnit);
let newBuf = buffer.from(buffer.from(message, 'utf-8').buffer);
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(newBuf.buffer) };
let encryptText = await AesUtils.encryptMessagePromise(symKey, plainText);
let encrypt = base64.encodeToStringSync(encryptText.data)
if (callBack) {
callBack(encrypt)
}
} catch (error) {
console.error(JSON.stringify(error));
if (callBack) {
let str = JSON.stringify(error)
callBack(str)
}
}
}
public static async decryptCBC(message: string, pk: string, callBack: Function) {
try {
let dataUnit = AesUtils.doMdBySync(pk)
let symKey = await AesUtils.genSymKeyByData(dataUnit);
let newBuf = buffer.from(buffer.from(message, 'utf-8').buffer);
let encryptText: cryptoFramework.DataBlob = { data: new Uint8Array(newBuf.buffer) };
let decryptText = await AesUtils.decryptMessagePromise(symKey, encryptText);
if (decryptText.data.toString()) {
let decrypt = buffer.from(decryptText.data).toString('utf-8')
if (callBack) {
callBack(decrypt)
}
} else {
if (callBack) {
callBack('decrypt failed')
}
}
} catch (error) {
if (callBack) {
let str = JSON.stringify(error)
callBack('decrypt failed' + str)
}
}
}
// key生成
private static async genSymKeyByData(symKeyData: Uint8Array) {
let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
let aesGenerator = cryptoFramework.createSymKeyGenerator('AES256');
let symKey = await aesGenerator.convertKey(symKeyBlob);
console.info('convertKey success:');
return symKey;
}
// 加密消息
private static async encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
let cipher = cryptoFramework.createCipher('AES256|CBC|PKCS7');
let iv = AesUtils.genIvParamsSpec();
await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv);
let cipherData = await cipher.doFinal(plainText);
return cipherData;
}
// 解密消息
private static async decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
let decoder = cryptoFramework.createCipher('AES256|CBC|PKCS7');
let iv = AesUtils.genIvParamsSpec();
await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv);
let decryptData = await decoder.doFinal(cipherText);
return decryptData;
}
private static doMdBySync(key: string): Uint8Array {
let mdAlgName = 'SHA256'; // 摘要算法名
let md = cryptoFramework.createMd(mdAlgName);
let firstArray = new Uint8Array(buffer.from(key, 'utf-8').buffer)
md.updateSync({ data: firstArray });
let mdResult = md.digestSync();
let data = new util.Base64Helper().encodeToStringSync(mdResult.data)
console.info('[Sync]:Md result base64:' + data);
let hex = buffer.from(mdResult.data).toString('hex')
console.info('Md result hex:' + hex);
let mdLen = md.getMdLength();
console.info("md len: " + mdLen);
return mdResult.data
}
// iv生成
private static genIvParamsSpec(): cryptoFramework.IvParamsSpec {
let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes
let dataIv = new Uint8Array(arr);
let ivBlob: cryptoFramework.DataBlob = { data: dataIv };
let ivParamsSpec: cryptoFramework.IvParamsSpec = {
algName: "IvParamsSpec",
iv: ivBlob
};
return ivParamsSpec;
}
}
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
HarmonyOS RAS加密,AES加密和解密
72浏览 • 1回复 待解决
HarmonyOS ArkTS中如何使用AES加密和解密
48浏览 • 1回复 待解决
HarmonyOS AES128|GCM 无法解密后台返回的加密数据
65浏览 • 1回复 待解决
使用AES解密时,一直在报解密异常
355浏览 • 1回复 待解决
HarmonyOS AES加密
48浏览 • 1回复 待解决
HarmonyOS AES加密相关
38浏览 • 1回复 待解决
HarmonyOS AES解密报错
720浏览 • 1回复 待解决
HarmonyOS AES加解密咨询
90浏览 • 1回复 待解决
HarmonyOS AES加解密问题
47浏览 • 1回复 待解决
HarmonyOS AES128加密和sm3国密加解密的方法
32浏览 • 1回复 待解决
HarmonyOS AES加密编码问题
83浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
614浏览 • 1回复 待解决
HarmonyOS AES加密算法demo
41浏览 • 1回复 待解决
HarmonyOS AES加密中文字符串,解密出来时空字符串
32浏览 • 1回复 待解决
使用HarmonyOS生成的SM2key加密成功,加密后的内容传到服务器报解密失败
476浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
226浏览 • 1回复 待解决
解密中文乱码 :CryptoJS鸿蒙版aes解密,中文乱码
1445浏览 • 2回复 待解决
HarmonyOS AES加解密与java无法通用
518浏览 • 1回复 待解决
HarmonyOS app Crash但是找不到错误原因
67浏览 • 1回复 待解决
HarmonyOS 请求参数的AES加密和RSA加密的实现方法
39浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
27浏览 • 1回复 待解决
MySQL 原生的aes_encrypt加密的Key为空字符串,怎么在代码里解密?
2193浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
68浏览 • 1回复 待解决
HarmonyOS AES128|CBC|NoPadding 加密报错
58浏览 • 1回复 待解决
HarmonyOS 压缩(.zip)文件进行加密问题,AES|CBC
91浏览 • 1回复 待解决
由于传入数据为base64加密,解码前要base64进行一次decodeSync。