HarmonyOS 无法正确生成

想生成AES256的密钥,但通过下面的接口获取的是乱码?如何正确获取String可以保存

let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256')
try {
  let skykey = await symKeyGenerator.generateSymKey();
  if (skykey && skykey.getEncoded().data) {
    return this.uint8ArrayToString(skykey.getEncoded().data);
  }

发现这样获取的key有乱码,如何能获取像这样的数据:

1234567890abcdef1234567890abcdef

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

参考以下代码:

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

@Entry
@Component
struct Page {
  build() {
    Column() {
      Text('测试')
        .padding(30)
        .onClick(()=>{
          testGenerateAesKey()
        })
    }
  }
}

async function testGenerateAesKey() {
  try {
    let lightWord = "This is a test";
    // 创建SymKeyGenerator实例
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256');
    let keyStart = symKeyGenerator.generateSymKeySync();
    // 转换
    let keyString = uint8ArrayToHexStr(keyStart.getEncoded().data)
    let keyData = HexStrTouint8Array(keyString)
    let key = await genSymKeyByData(keyData)
    // 加密
    let cipher = cryptoFramework.createCipher('AES256|PKCS7');
    cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, key, null);
    let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(lightWord, 'utf-8').buffer) };
    let encryptText =  await cipher.doFinal(plainText);
    // 解密
    let decoder = cryptoFramework.createCipher('AES256|PKCS7');
    decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, key, null);
    let decryptText = await decoder.doFinal(encryptText)
    if (plainText.data.toString() === decryptText.data.toString()) {
      console.info('decrypt ok');
      console.info('解密成功 plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
    } else {
      console.error('decrypt failed');
    }
  }catch (error) {
    console.error('报错;',JSON.stringify(error));
  }
}

async function genSymKeyByData(symKeyData: Uint8Array) {
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
  let aesGenerator = cryptoFramework.createSymKeyGenerator('AES256');
  let symKey = await aesGenerator.convertKey(symKeyBlob);
  return symKey;
}

function HexStrTouint8Array(data: string): Uint8Array {
  return new Uint8Array(buffer.from(data, 'hex').buffer);
}

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 Navigation无法正确显示
49浏览 • 1回复 待解决
HarmonyOS 平板无法正确横竖屏切换
13浏览 • 1回复 待解决
HarmonyOS px2vp在模拟器上无法正确转换
540浏览 • 1回复 待解决
HarmonyOS 如何生成UUID?
322浏览 • 1回复 待解决
HarmonyOS 组件生成图片保存
31浏览 • 1回复 待解决
HarmonyOS 生成海报的组件
59浏览 • 1回复 待解决
HarmonyOS so库的正确引用方式
33浏览 • 1回复 待解决
HarmonyOS上如何正确的获取OAID
15浏览 • 1回复 待解决
HarmonyOS webview如何生成pdf
20浏览 • 1回复 待解决
HarmonyOS KeyPair 生成证书失败问题
23浏览 • 1回复 待解决
HarmonyOS har包生成问题
1186浏览 • 1回复 待解决