使用AES CBC模式加解密,convertKey报错401

使用AES CBC模式加解密,convertKey报错401。

HarmonyOS
2024-11-08 11:09:43
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

32字节的key需要使用AES256,请参考以下demo:

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import { BusinessError } from '@ohos.base'; 
import buffer from '@ohos.buffer'; 
import util from '@ohos.util'; 
 
@Entry 
@Component 
struct AES_InKey { 
  @State message: string = 'Hello World' 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold).onClick(() => { 
          let aes = new AES_CBC(); 
          aes.do(); 
        }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
let key = 'lkanaaiWY8dfqDrwpNDQXMhKlJyuKuwF' 
let base = new util.Base64Helper(); 
 
export class AES_CBC { 
  //加密 
  async aesEncrypt(text: string, key: string): Promise<string> { 
    let globalResult = "" 
    try { 
      let cipherAlgName = 'AES256|CBC|PKCS5'; 
      let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
 
      let symAlgName = 'AES256'; 
      let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
      //let dataUint8Array = base.decodeSync(key) 
      // let dataUint8Array =HexStrTouint8Array(key) 
      let dataUint8Array = stringToUint8Array(key) 
      let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
      let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
 
      let ivParam: cryptoFramework.IvParamsSpec = { 
        algName: 'IvParamsSpec', 
        iv: { 
          data: stringToUint8Array('aaaaaaaaaaaaaaaa') 
        } 
      } 
      await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, ivParam); 
      let plainText: cryptoFramework.DataBlob = { data: stringToUint8Array(text) } 
      let result = await globalCipher.doFinal(plainText) 
      globalResult = base.encodeToStringSync(result.data); 
      console.log("test---" + globalResult) 
    } catch (err) { 
      console.log(err.message) 
    } 
    return globalResult; 
  } 
 
  // 解密 
  async aesDecrypt(text: string, key: string) { 
    let globalResult = "" 
    try { 
      let cipherAlgName = 'AES256|CBC|PKCS5'; 
      let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
 
      let symAlgName = 'AES256'; 
      let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
 
      //let dataUint8Array = base.decodeSync(key) 
      //let dataUint8Array =HexStrTouint8Array(key) 
      let dataUint8Array = stringToUint8Array(key) 
      let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
      let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
      let ivParam: cryptoFramework.IvParamsSpec = { 
        algName: 'IvParamsSpec', 
        iv: { 
          data: stringToUint8Array('aaaaaaaaaaaaaaaa') 
        } 
      } 
 
      await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, ivParam); 
      let plainText: cryptoFramework.DataBlob = { data: base.decodeSync(text) } 
      let result = await globalCipher.doFinal(plainText) 
      globalResult = uint8ArrayToString(result.data); 
      console.log("test---解密==》" + globalResult) 
      AlertDialog.show({ message: '解密==》' + globalResult }) 
    } catch (err) { 
      console.log(err.message) 
    } 
  } 
 
 
  async do() { 
    //加密 
    let res = await this.aesEncrypt("/data/app/el2/100/base/cn.tf.mobilebankhm/haps/entry/files/modules/aa十大", key); 
    //解密 
    this.aesDecrypt(res, key); 
  } 
} 
 
// 字符串转成字节流 
function stringToUint8Array(str: string) { 
  return new Uint8Array(buffer.from(str, 'utf-8').buffer); 
} 
 
 
// 字节流转成可理解的字符串 
function uint8ArrayToString(array: Uint8Array) { 
  // 将UTF-8编码转换成Unicode编码 
  let out: string = ""; 
  let index: number = 0; 
  let len: number = array.length; 
  while (index < len) { 
    let character = array[index++]; 
    switch (character >> 4) { 
      case 0: 
      case 1: 
      case 2: 
      case 3: 
      case 4: 
      case 5: 
      case 6: 
      case 7: 
        out += String.fromCharCode(character); 
        break; 
      case 12: 
      case 13: 
        out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F)); 
        break; 
      case 14: 
        out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) | 
          ((array[index++] & 0x3F) << 0)); 
        break; 
      default: 
        break; 
    } 
  } 
  return out; 
} 
 
//十六进制转Uint8Array 
function HexStrTouint8Array(data: string): Uint8Array { 
  return new Uint8Array(buffer.from(data, 'hex').buffer); 
} 
 
function uint8ArrayToHexStr(data: Uint8Array): string { 
  let hexString = ""; 
  let i: number; 
  for (i = 0; i < data.length; i++) { 
    let char = ('00' + data[i].toString(16)).slice(-2); 
    hexString += char; 
  } 
  return hexString; 
}
分享
微博
QQ
微信
回复
2024-11-08 16:27:59
相关问题
如何使用SM4的CBC模式加解密
775浏览 • 1回复 待解决
SM4 CBC模式加解密,有好的方案吗?
930浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
386浏览 • 1回复 待解决
HarmonyOS AES解密报错
501浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
484浏览 • 1回复 待解决
如何进行不同规格的AES加解密
690浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
1990浏览 • 1回复 待解决
使用32字节秘钥加解密报错
1572浏览 • 1回复 待解决
SM4采用OFB模式进行加解密
799浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
370浏览 • 1回复 待解决
加解密算法库框架使用
802浏览 • 1回复 待解决
使用AES解密时,一直在报解密异常
98浏览 • 1回复 待解决
HarmonyOS 加解密 demo
392浏览 • 1回复 待解决