HarmonyOS 如何实现AES_128_ECB_PKCS5Padding加解密算法?

加密算法:

输入:

key: 加密key,16位的普通字符串

content:待加密内容,普通字符串

输出:

result:加密之后的内容,base64编码的字符串

解密算法:

输入:

key: 加密key,16位的普通字符串

content:待解密内容,base64编码的字符串

输出:

result:解密之后的内容,普通字符串

HarmonyOS
2024-08-13 14:52:04
2132浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

请参考下述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 = 'bb28xxxxf96-f8e4-42' 
let iv = "6c5bd6e53718xxxx9980ad0b04d22e7c" 
 
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; 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
分享
微博
QQ
微信
回复
2024-08-13 21:00:12


相关问题
HarmonyOS 加解密方式 AES/ECB/PKCS5Padding
1549浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
885浏览 • 1回复 待解决
HarmonyOS 加解密算法匹配
1125浏览 • 1回复 待解决
HarmonyOS 加解密算法如何使用
1053浏览 • 1回复 待解决
HarmonyOS AES加解密问题
1195浏览 • 1回复 待解决
HarmonyOS AES加解密咨询
1358浏览 • 1回复 待解决
HarmonyOS AES-CBC加解密
1135浏览 • 1回复 待解决
aes-128加密问题如何实现
1085浏览 • 1回复 待解决
加解密算法库框架使用
1910浏览 • 1回复 待解决
HarmonyOS AES加解密报错17630001
1456浏览 • 1回复 待解决
是否有AESECB模式的加密算法
1569浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
1510浏览 • 1回复 待解决
如何进行不同规格的AES加解密
1918浏览 • 1回复 待解决
基于加解密算法框架的规格问题
1710浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
1128浏览 • 1回复 待解决