简介:本文介绍了将Android AES加解密功能移植到鸿蒙系统的实现过程。代码展示了使用ArkTS语言开发的CipherModel类,包含AES-128 CBC模式加解密功能,支持PKCS5/PKCS7填充。主要实现字符串与Uint8Array的转换、密钥处理(截取前16字节作为密钥,后续作为IV)、基于cryptoFramework的加密/解密流程,并通过Base64进行数据编码/解码转换。使用示例展示了如何调用aesEncrypt方法进行加密操作。该实现保留了与Android版本相似的功能接口,同时适配了鸿蒙系统的加解密API。
之前项目时android的现在在尝试移植开发一个鸿蒙原生的,中间碰到AES加解密,根据原有的android代码,转换为ArcTS。代码如下:
import util from '@ohos.util';
import cryptoFramework from '@ohos.security.cryptoFramework';
import { Logger } from './Logger';
const TAG: string = '[CipherModel]'
const AES128: string = 'AES128';
const AES128_CBC_PKCS5: string = 'AES128|CBC|PKCS5';
const AES128_PKCS7: string = 'AES128|PKCS7';
/**
* AES加密,解密模块
*/
export class CipherModel {
stringToUint8Array(str:string) {
let arr:Array<number> = [];
for (let i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
let tmpArray = new Uint8Array(arr);
return tmpArray;
}
uint8ArrayToString(array: Uint8Array) {
let arrayString = '';
for (let i = 0; i < array.length; i++) {
arrayString += String.fromCharCode(array[i]);
}
return arrayString;
}
aesEncrypt(message: string, key: string, callback:(result:string)=>void) {
let that = new util.Base64Helper();
if(key.length>16){
key = key.substring(0,16)
}
let iv = key.substring(16)
let paramsSpec: cryptoFramework.IvParamsSpec = { iv: { data: this.stringToUint8Array(iv) }, algName: "IvParamsSpec" }
let aesGenerator = cryptoFramework.createSymKeyGenerator(AES128);
let cipher = cryptoFramework.createCipher(AES128_CBC_PKCS5);
let pubKey = that.decodeSync(key);
let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKey };
aesGenerator.convertKey(pubKeyBlob, (err, symKey) => {
if (err) {
console.error("convertKey: error.");
return;
}
cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, paramsSpec, (err, data) => {
let input: cryptoFramework.DataBlob = { data: this.stringToUint8Array(message) };
cipher.doFinal(input, (err, data) => {
Logger.info(TAG, "EncryptOutPut is " + data.data);
let result = that.encodeToStringSync(data.data)
Logger.info(TAG, "result is " + result);
callback(result)
})
})
})
}
aesDecrypt(message: string, key: string, callback:(result:string)=>void) {
if(key.length>16){
key = key.substring(0,16)
}
let iv = key.substring(16)
let paramsSpec: cryptoFramework.IvParamsSpec = { iv: { data: this.stringToUint8Array(iv) }, algName: "IvParamsSpec" }
let aesGenerator = cryptoFramework.createSymKeyGenerator(AES128);
let cipher = cryptoFramework.createCipher(AES128_CBC_PKCS5);
let that = new util.Base64Helper();
let pubKey = that.decodeSync(key);
let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKey };
aesGenerator.convertKey(pubKeyBlob, (err, symKey) => {
if (err) {
console.error("convertKey: error.");
return;
}
cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, paramsSpec, (err, data) => {
let newMessage = that.decodeSync(message);
let input: cryptoFramework.DataBlob = { data: newMessage };
cipher.doFinal(input, (err, data) => {
Logger.info(TAG, "DecryptOutPut is " + data.data);
let result = this.uint8ArrayToString(data.data)
Logger.info(TAG, "result is " + result);
callback(result)
})
})
})
}
}
其中Key是密钥,每次加解密需要输入对应的密钥,自己可以自定义,使用方法:
this.cipherModel.aesEncrypt(//加密
this.newPwdStr,//加密内容
CommonConstants.sKey,//加密密钥
(result: string) => {
Logger.info("加密", `this result = ${JSON.stringify(result)}`);
this.cipherText = result;//加密结果
})