HarmonyOS 鸿蒙Next 在API9的项目中无法使用crypto-js,怎样才能正确导入?
HarmonyOS 鸿蒙Next 在API9的项目中无法使用crypto-js,怎样才能正确导入?
通过npm安装了 crypto-js
,但是在引入时报错。入下图所示,这个该怎么引入呢?
还使用过:ohpm install @ohos/crypto-js
,这个可以引入,但是不支持API 9。
麻烦问下大家怎样才能正确导入crypto-js
?
更多关于HarmonyOS 鸿蒙Next 在API9的项目中无法使用crypto-js,怎样才能正确导入?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
后来参考文章自己写了一个AES加解密的方法
参考地址:https://blog.csdn.net/xo19882011/article/details/133885770
import cryptoFramework from '@ohos.security.cryptoFramework';
import util from '@ohos.util';
// AES 参考地址:https://blog.csdn.net/xo19882011/article/details/133885770
/**
*
* @param plaintext
* @param key
* @param iv
* @returns
*/
export function aesCBCEncrypt(plaintext, key, iv) {
let cipherAlgName = 'AES128|CBC|NoPadding';
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)
})
})
}
export function aesCBCDecrypt(encrypttext, key, iv) {
let cipherAlgName = 'AES128|CBC|NoPadding';
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 error = " + JSON.stringify(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 => {
console.log(`output=${output}`);
let result = uint8ArrayToString(output.data)
console.log(`output.result=${result}`);
return new Promise(resolve => {
resolve(result)
})
}).catch(err => {
console.log(`output err =${err}`);
return new Promise((_, reject) => {
reject(err)
})
})
}).catch(err => {
console.log(`convertKey err =${err}`);
return new Promise((_, reject) => {
reject(err)
})
})
}
// 把密钥、明文等转换成输入数据需要的格式
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);
}
// 解密内容转换成字符串
// 这里替换了参考文章中的方法,支持中文。
function uint8ArrayToString(array) {
let textDecoder = util.TextDecoder.create("utf-8",{ignoreBOM: true});
return textDecoder.decodeWithStream(array);
}
更多关于HarmonyOS 鸿蒙Next 在API9的项目中无法使用crypto-js,怎样才能正确导入?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
试过这种,因为版本是2.0.2不支持API9,手动修改版本为2.0.0还是不行,
在HarmonyOS鸿蒙Next的API9项目中,无法直接使用crypto-js库,主要是因为鸿蒙的JS API与传统的Web环境有所不同。要正确导入crypto-js,可以通过以下步骤实现:
-
使用npm安装crypto-js:首先,确保你的项目已经配置了npm环境,然后通过命令行安装crypto-js库。
npm install crypto-js
-
引入crypto-js模块:在需要使用crypto-js的JS文件中,通过
require
或import
语句引入模块。const CryptoJS = require('crypto-js');
-
使用crypto-js功能:引入模块后,可以正常使用crypto-js提供的加密解密功能。例如:
const encrypted = CryptoJS.AES.encrypt('Message', 'Secret Passphrase'); const decrypted = CryptoJS.AES.decrypt(encrypted, 'Secret Passphrase');
-
处理兼容性问题:如果遇到鸿蒙API与crypto-js不兼容的情况,可能需要手动调整crypto-js的部分代码,或者寻找鸿蒙原生支持的加密库作为替代。
通过以上步骤,可以在鸿蒙Next的API9项目中正确导入并使用crypto-js库。