HarmonyOS AES加密/RSA加密问题

1、生成AES密钥时服务端加解密使用模型AES/ECB/PKCS5Padding,但似乎文档cryptoFramework.createCipher时未提供相关模式,这就导致无法进行加解密。

2、随机生成16位秘钥时,通过cryptoFramework.createSymKeyGenerator进行转换时,生成AES密钥会失败。RSA在创建秘钥、加解密时有同样的问题。

HarmonyOS
2025-01-10 08:14:09
730浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以参考下述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 = 'bb28bf96-f8e4-42'
let iv = "6c5bd6e5371847769980ad0b04d22e7c"

let base = new util.Base64Helper();
export class AES_CBC {
  async do() {
    //加密
    let res = await this.aesEncrypt("xxxxx", 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 = stringToUint8Array(key)
      let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array }
      let promiseSymKey = await symKeyGenerator.convertKey(keyBlob)
      let globalCbcParams = this.genCBCParamsSpec()
      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 = stringToUint8Array(key)
      let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array }
      let promiseSymKey = await symKeyGenerator.convertKey(keyBlob)

      // /*设置偏移量 */
      let globalCbcParams = this.genCBCParamsSpec();
      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.
分享
微博
QQ
微信
回复
2025-01-10 09:29:30


相关问题
HarmonyOS AES加密编码问题
500浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
1089浏览 • 1回复 待解决
HarmonyOS RSA加密失败问题
674浏览 • 1回复 待解决
HarmonyOS AES加密
543浏览 • 1回复 待解决
HarmonyOS RAS加密AES加密和解密
558浏览 • 1回复 待解决
HarmonyOS RSA加密中,公私钥问题
768浏览 • 1回复 待解决
HarmonyOS RSA加密方式
1334浏览 • 1回复 待解决
HarmonyOS AES加密相关
539浏览 • 1回复 待解决
AES 加密问题,有知道的吗?
959浏览 • 1回复 待解决
aes-128加密问题如何实现?
705浏览 • 1回复 待解决
HarmonyOS rsa加密报错
712浏览 • 1回复 待解决
HarmonyOS RSA分段加密乱码
1299浏览 • 1回复 待解决
HarmonyOS 实现RSA加密功能
957浏览 • 1回复 待解决
HarmonyOS 使用AES加密失败
668浏览 • 1回复 待解决
RSA NoPadding模式加密失败
1989浏览 • 1回复 待解决
HarmonyOS rsa加密返回401错误
636浏览 • 1回复 待解决
HarmonyOS RSA加密算法实现
837浏览 • 1回复 待解决
HarmonyOS AES加密算法demo
788浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。