HarmonyOS AES加解密第三方开源工具类库-鸿蒙开发者社区-51CTO.COM

HarmonyOS AES加解密第三方开源工具类库

是否有AES加解密第三方开源工具类库, 我使用官方文档里的AES加密,遇到了aesGenerator.convertKey不返回的情况,请问是否有第三方库呢

HarmonyOS
2024-08-13 15:03:27
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

请参考下述demo,并与自身实现进行适配

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
import util from '@ohos.util'; 
 
@Entry 
@Component 
struct Index { 
  @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 = 'bb2xxxxf96-f8e4-42' 
let iv = "6c5xxxxx371847769xxxx0b04d22e7c" 
 
let base = new util.Base64Helper(); 
export class AES_CBC { 
  async do() { 
    //加密 
    let res = await this.aesEncrypt("枯藤老树昏鸦", key); 
    //解密 
    this.aesDecrypt(res, key); 
  } 
 
  //加密 
  async aesEncrypt(text: string, key: string): Promise<string> { 
    let globalResult = "" 
    try { 
      let cipherAlgName = 'AES128|ECB|PKCS5'; 
      let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
 
      let symAlgName = 'AES128'; 
      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 globalCbcParams = this.genCBCParamsSpec() 
      //globalCbcParams.iv = { data: stringToUint8Array(iv) } 
      globalCbcParams.iv = { data: HexStrTouint8Array(iv) } 
      await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, globalCbcParams); 
      let plainText: cryptoFramework.DataBlob = { data: stringToUint8Array(text) } 
      let result = await globalCipher.doFinal(plainText) 
      globalResult = base.encodeToStringSync(result.data); 
      let s = uint8ArrayToString(result.data); 
    } catch (err) { 
      console.log(err.message) 
    } 
    return globalResult; 
  } 
  // 解密 
  async aesDecrypt(text: string, key: string) { 
    let globalResult = "" 
    try { 
      let cipherAlgName = 'AES128|ECB|PKCS5'; 
      let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
 
      let symAlgName = 'AES128'; 
      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 globalCbcParams = this.genCBCParamsSpec(); 
      //globalCbcParams.iv = { data: stringToUint8Array(iv) } 
      globalCbcParams.iv = { data: HexStrTouint8Array(iv) } 
      await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, globalCbcParams); 
      let plainText: cryptoFramework.DataBlob = { data: base.decodeSync(text) } 
      let result = await globalCipher.doFinal(plainText) 
      globalResult = uint8ArrayToString(result.data); 
      AlertDialog.show({ message: '解密==》' + globalResult }) 
      console.log("test---解密==》" + globalResult) 
    } catch (err) { 
      console.log(err.message) 
    } 
  } 
  genCBCParamsSpec() { 
    let arr = [0x36, 0x61, 0x38, 0x77, 0x30, 0x63, 0x39, 0x31, 0x6b, 0x63, 0x37, 0x45, 0x55, 0x44, 0x57, 0x46]; 
    let dataIv = new Uint8Array(arr); 
    let ivBlob: cryptoFramework.DataBlob = { 
      data: dataIv 
    }; 
 
    let cbcParamsSpec: cryptoFramework.IvParamsSpec = { 
      iv: ivBlob, 
      algName: 'IvParamsSpec' 
    }; 
    return cbcParamsSpec; 
  } 
} 
 
// 字符串转成字节流 
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); 
} 
 
//Uint8Array转y十六进制 
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-08-13 22:15:54
相关问题
native交叉编译第三方
68浏览 • 1回复 待解决
如何引入自己的“第三方
430浏览 • 1回复 待解决
HarmonyOS支持的第三方的列表
1288浏览 • 1回复 待解决
HarmonyOS如何移植第三方MCU?
9064浏览 • 1回复 待解决
第三方怎么装鸿蒙系统?
2212浏览 • 1回复 待解决
开发第三方SDK如何编译?
13412浏览 • 2回复 待解决
react-native 第三方使用npm更新
1247浏览 • 1回复 待解决
第三方js迁移有懂的吗?
2297浏览 • 1回复 待解决
ArkTS不支持使用第三方的js
4041浏览 • 1回复 待解决
HarmonyOS 第三方应用安装和卸载广播
316浏览 • 0回复 待解决
HarmonyOS第三方sdk bugly平替方案
335浏览 • 1回复 待解决
图片剪切的第三方框架
89浏览 • 1回复 待解决
OHPM包管理怎么安装第三方
4332浏览 • 1回复 待解决