HarmonyOS 鸿蒙Next AES加密第一次以后都报错401
HarmonyOS 鸿蒙Next AES加密第一次以后都报错401
cryptoFramework.Cipher创建成功后,加密数据,首次调用下面的encrypt方法是成功的,后面就都报{“code”:401}错误,这个为啥呢?
init();
encrypt("lhofiewhofewhofJjh哈哈哈哈“);
encrypt(“lhofiewhofewhofJjhdddd哈“);
encrypt(“lhofiewhofewhofJjh哈ssssss哈“);
init() {
if(this.initialed){
return;
}
let symKeyGenerator = cryptoFramework.createSymKeyGenerator(this.algorithmWithLength);
let array: Uint8Array = StringUtils.stringToUint8Array(this.password)
let keyMaterialBlob: cryptoFramework.DataBlob = { data: array }
try {
symKeyGenerator.convertKey(keyMaterialBlob, (error, key) => {
if (error) {
let e: BusinessError = error as BusinessError;
console.error(
return;
}
this.aesKey = key; // 获取对称密钥的二进制数据,并以字节数组形式输出。
console.info(
try {
this.cryptoCipher = cryptoFramework.createCipher(this.algorithmWithLength + ‘|’
+ this.blockCipherMode + ‘|’
+ this.paddingMode);
this.cryptoCipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, this.aesKey, this.genIvParamsSpec());
this.initialed = true;
console.info(
} catch (e) {
console.error(
this.initialed = false;
}
})
} catch (error) { // 参数检查发现错误立即抛出异常
let e: BusinessError = error as BusinessError;
Logger.error(
this.initialed = false;
}
}
encrypt(text:string|null):string{
if(!text){
return “”;
}
if(this.initialed && this.cryptoCipher!=null){
try {
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(text, ‘utf-8’).buffer) }
console.info(
let encryptText = this.cryptoCipher.doFinalSync(plainText);
let str= Base64.encodeArray(encryptText.data).replaceAll(”[\s*\n\r]”, “");
console.info(
}catch (e){
console.error(
return text.replaceAll("[\s\n\r]”, “");
}
}else{
console.error(
return text.replaceAll("[\s\n\r]”, “*”);
}
}
init();
encrypt("lhofiewhofewhofJjh哈哈哈哈“);
encrypt(“lhofiewhofewhofJjhdddd哈“);
encrypt(“lhofiewhofewhofJjh哈ssssss哈“);
init() {
if(this.initialed){
return;
}
let symKeyGenerator = cryptoFramework.createSymKeyGenerator(this.algorithmWithLength);
let array: Uint8Array = StringUtils.stringToUint8Array(this.password)
let keyMaterialBlob: cryptoFramework.DataBlob = { data: array }
try {
symKeyGenerator.convertKey(keyMaterialBlob, (error, key) => {
if (error) {
let e: BusinessError = error as BusinessError;
console.error(
convertKey error, ${e.code}, ${e.message}
, ‘AESLog’);return;
}
this.aesKey = key; // 获取对称密钥的二进制数据,并以字节数组形式输出。
console.info(
aes key hex: ${this.aesKey.getEncoded().data}
, ‘AESLog’);try {
this.cryptoCipher = cryptoFramework.createCipher(this.algorithmWithLength + ‘|’
+ this.blockCipherMode + ‘|’
+ this.paddingMode);
this.cryptoCipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, this.aesKey, this.genIvParamsSpec());
this.initialed = true;
console.info(
cryptoCipher init success
, ‘AESLog’);} catch (e) {
console.error(
createCipher error, ${e.code}, ${e.message}
, ‘AESLog’);this.initialed = false;
}
})
} catch (error) { // 参数检查发现错误立即抛出异常
let e: BusinessError = error as BusinessError;
Logger.error(
convertKey failed, ${e.code}, ${e.message}
, ‘AESLog’);this.initialed = false;
}
}
encrypt(text:string|null):string{
if(!text){
return “”;
}
if(this.initialed && this.cryptoCipher!=null){
try {
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(text, ‘utf-8’).buffer) }
console.info(
encrypt before:${plainText.data.toString()}
,‘AESLog’)let encryptText = this.cryptoCipher.doFinalSync(plainText);
let str= Base64.encodeArray(encryptText.data).replaceAll(”[\s*\n\r]”, “");
console.info(
encrypt success:${str},origin:${text}
,‘AESLog’)}catch (e){
console.error(
encrypt error:${JSON.stringify(e)},text:${text}
,‘AESLog’)return text.replaceAll("[\s\n\r]”, “");
}
}else{
console.error(
encrypt initialed false or cryptoCipher is null
,‘AESLog’)return text.replaceAll("[\s\n\r]”, “*”);
}
}
1 回复
更多关于HarmonyOS 鸿蒙Next AES加密第一次以后都报错401的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对您提出的HarmonyOS 鸿蒙Next AES加密第一次以后都报错401的问题,这通常指示着在加密过程中存在参数错误或加密环境未正确设置。以下是一些可能的解决方案:
- 检查密钥和算法参数:确保每次加密使用的密钥和算法参数(如AES128、CBC模式等)一致且正确。若密钥或参数在第一次加密后有所更改,请恢复为初始设置。
- 初始化向量(IV):对于CBC等需要IV的加密模式,请确保每次加密都使用随机生成的IV,并且IV在加密过程中未被篡改或重用。
- 加密环境:验证加密环境是否稳定,包括系统资源、内存使用等,确保加密操作未受到外部干扰。
- 错误处理:检查加密代码中的错误处理逻辑,确保在出现错误时能够正确捕获并处理,避免错误累积导致后续加密失败。
如果上述方法均无法解决问题,请考虑联系官网客服以获取更专业的技术支持。官网地址是:https://www.itying.com/category-93-b0.html。