HarmonyOS AES加密/RSA加密问题

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

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

HarmonyOS
2天前
浏览
收藏 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;
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS RSA加密失败问题
172浏览 • 1回复 待解决
HarmonyOS AES加密编码问题
197浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
721浏览 • 1回复 待解决
HarmonyOS RAS加密AES加密和解密
160浏览 • 1回复 待解决
HarmonyOS AES加密
143浏览 • 1回复 待解决
HarmonyOS RSA加密中,公私钥问题
277浏览 • 1回复 待解决
HarmonyOS RSA加密方式
939浏览 • 1回复 待解决
HarmonyOS AES加密相关
153浏览 • 1回复 待解决
HarmonyOS 使用AES加密失败
302浏览 • 1回复 待解决
HarmonyOS 实现RSA加密功能
680浏览 • 1回复 待解决
HarmonyOS rsa加密报错
325浏览 • 1回复 待解决
HarmonyOS RSA分段加密乱码
876浏览 • 1回复 待解决
AES 加密问题,有知道的吗?
359浏览 • 1回复 待解决
aes-128加密问题如何实现?
333浏览 • 1回复 待解决
HarmonyOS AES加密算法demo
223浏览 • 1回复 待解决
RSA NoPadding模式加密失败
1532浏览 • 1回复 待解决
HarmonyOS rsa加密返回401错误
236浏览 • 1回复 待解决
HarmonyOS RSA加密算法实现
232浏览 • 1回复 待解决
HarmonyOS 关于base64和aes加密相关问题
160浏览 • 1回复 待解决