HarmonyOS 是否支持根据指定私钥解析文件内容并进行xml解析

下载到沙箱中的 xml文件内容是加密过的,是否支持根据指定私钥解析文件内容并进行xml解析

HarmonyOS
7天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

可以参考这个demo,把xml文件放到entry模块的rawfile文件夹下面:

import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';


let key = 'Q0Rza0M4VGJjNVhwYTBSUg=='
let cipherAlgName = 'AES128|ECB|PKCS7';
let symAlgName = 'AES128';
let base64Helper = new util.Base64Helper;

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';



  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
      Button('加解密数据')
        .onClick(() => {
          //获取加密文件
          let xmlContent: Uint8Array = getContext().resourceManager.getRawFileContentSync("mibook.xml");
          let plainText: cryptoFramework.DataBlob = { data: xmlContent };

          AES_CBC_Update(plainText)
        })
    }
    .height('100%')
    .width('100%')
  }
}
async function AES_CBC_Update(plainText: cryptoFramework.DataBlob) {
  //解密
  await aesDecrypt(plainText, key);
}

// 解密
async function aesDecrypt(globalCipherText: cryptoFramework.DataBlob, key: string) {
  let globalPlainText = "";
  try {
    let globalCipher = cryptoFramework.createCipher(cipherAlgName);
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName);

    let dataUint8Array = base64Helper.decodeSync(key)
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array }
    /*
    * 偏移量
    * */
    let cbcParamsSpec: cryptoFramework.IvParamsSpec = {
      iv: { data: stringToUint8Array("your-iv-your-iva") },
      algName: 'IvParamsSpec'
    };

    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob)
    await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, null);
    let updateLength = 1024;
    for (let i = 0; i <= globalCipherText.data.length / updateLength; i++) {
      let messageArr = globalCipherText.data.slice(i * updateLength, (i + 1) * updateLength);
      let message = new Uint8Array(messageArr);
      let messageBlob: cryptoFramework.DataBlob = { data: message };
      let updateOutput = await globalCipher.update(messageBlob);
      if (updateOutput) {
        globalPlainText += uint8ArrayToString(updateOutput.data);
      }
    }
    let finalOutput = await globalCipher.doFinal(null);


    console.log("解密后的明文==》" + globalPlainText)
    console.log("解密后的明文==》" + globalPlainText.length)
  } catch (err) {
    console.log(err.message)
  }
}

// 字符串转成字节流
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;
}
分享
微博
QQ
微信
回复
7天前
相关问题
HarmonyOS 如何解析xml文件
113浏览 • 1回复 待解决
HarmonyOS xml解析异常
86浏览 • 1回复 待解决
HarmonyOS xml解析工具
51浏览 • 1回复 待解决
xml文件数据解析 ,都有哪些步骤?
822浏览 • 1回复 待解决
HarmonyOS 怎么解析本地文件json内容
243浏览 • 1回复 待解决
鸿蒙 | Jar包中解析xml文件为空值。
7583浏览 • 1回复 待解决
HarmonyOS stringify是否支持对Map的解析
135浏览 • 1回复 待解决
HarmonyOS abc文件是否解析
89浏览 • 1回复 待解决
怎样实现XML标签和标签值的解析
374浏览 • 1回复 待解决
HarmonyOS 文件上传无法解析文件地址
139浏览 • 1回复 待解决
HarmonyOS 如何解析zip文件
57浏览 • 1回复 待解决