本文主要是介绍鸿蒙ArkTS Api9 AES CBC 加密,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
鸿蒙ArkTS Api9 AES CBC 加密
由于鸿蒙ArkTS是javascript编写的,因此总是感觉会被抓包破解了,不过这个问题通过提了个工单问了一下,目前鸿蒙编译完成之后的包是无法获取到其内部代码的。因此就能够放心的把加密的密钥给扔在里边了。
所以研究了一下目前的鸿蒙加密算法怎么实现。
以下以 AES 128位 CBC 模式 加密为例。
密钥不足的时候以 0x00 填充。
加解密算法库框架
加解密算法库框架开发指导
参考文档
加密部分
结合当前鸿蒙的编码方式,代码流程如下:
- 转换可用密钥
- 生成iv
- 根据密钥生成加密器
- 加密
整体代码如下:
import cryptoFramework from '@ohos.security.cryptoFramework';
import util from '@ohos.util';// 把密钥、明文等转换成输入数据需要的格式
function stringToUint8Array(str, len=null) {let arr = [];if (len == null) {len = str.length}for (let i = 0; i < len; i++) {if (str.length > i) {arr.push(str.charCodeAt(i))} else {arr.push(0)}}return new Uint8Array(arr);
}// promise形式调用
function aesCBCEncrypt(plaintext, key, iv) {let cipherAlgName = 'AES128|CBC|PKCS7';let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128')let ivParam: cryptoFramework.IvParamsSpec = {algName: 'IvParamsSpec',iv: {data: stringToUint8Array(iv, 16)}}var cipher;return symKeyGenerator.convertKey({data: stringToUint8Array(key, 16)}).then(symKey => {try {cipher = cryptoFramework.createCipher(cipherAlgName);console.info(`xx cipher algName: ${cipher.algName}`);} catch (error) {console.error(`xx createCipher failed, ${error.code}, ${error.message}`);return null}return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, ivParam).then(() => {return cipher.doFinal({data: stringToUint8Array(plaintext)})}).then(output => {let base64 = new util.Base64Helper();let result = base64.encodeToStringSync(output.data);return new Promise((resolve) => {resolve(result)})}).catch(err => {return new Promise((_, reject) => {reject(err)})})}).catch(err => {return new Promise((_, reject) => {reject(err)})})
}
调用方式:
aesCBCEncrypt('000','1111000000000000','0000000000000000').then(res=>{console.log('aesCBCEncrypt is ' + res)
}).catch(err => {console.log('aesCBCEncrypt catch ' + err)
})// ===> MXROfa/ujWkb69CTvl8aig==
解密部分
结合当前鸿蒙的编码方式,代码流程如下:
- 转换可用密钥
- 生成iv
- 根据密钥生成加密器
- 解密
整体代码如下:
import cryptoFramework from '@ohos.security.cryptoFramework';
import util from '@ohos.util';// 解密内容转换成字符串
function uint8ArrayToString(array) {let arrayString = '';for (let i = 0; i < array.length; i++) {arrayString += String.fromCharCode(array[i]);}return arrayString;
}function aesCBCDecrypt(encrypttext, key, iv) {let cipherAlgName = 'AES128|CBC|PKCS7';let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128')let ivParam: cryptoFramework.IvParamsSpec = {algName: 'IvParamsSpec',iv: {data: stringToUint8Array(iv, 16)}}var cipher;return symKeyGenerator.convertKey({data: stringToUint8Array(key, 16)}).then(symKey => {try {cipher = cryptoFramework.createCipher(cipherAlgName);console.info(`xx cipher algName: ${cipher.algName}`);} catch (error) {console.error(`xx createCipher failed, ${error.code}, ${error.message}`);return null}return cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, ivParam).then(() => {let base64 = new util.Base64Helper();let result = base64.decodeSync(encrypttext);return cipher.doFinal({data: result})}).then(output => {let result = uint8ArrayToString(output.data)return new Promise((resolve) => {resolve(result)})}).catch(err => {return new Promise((_, reject) => {reject(err)})})}).catch(err => {return new Promise((_, reject) => {reject(err)})})
}
调用方式如下:
aesCBCDecrypt('MXROfa/ujWkb69CTvl8aig==','1111000000000000','0000000000000000').then(res=>{console.log('aesCBCDecrypt is ' + res)
}).catch(err => {console.log('aesCBCDecrypt catch ' + err)
})
这篇关于鸿蒙ArkTS Api9 AES CBC 加密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!