HarmonyOS 鸿蒙Next 调用convertKey生成密钥后返回内容unknown

发布于 1周前 作者 wuwangju 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 调用convertKey生成密钥后返回内容unknown

使用cryptoFramework的sm4加密功能时,在使用指定数据生成对称密钥使用了convertKey后,返回的结果通过PC端打印出来均为unknown

这是打印的结果:

{"format":"[PC Preview] unknown format","algName":"[PC Preview] unknown algName"}
3 回复

同问,使用文档样例也是返回同样的结果。

更多关于HarmonyOS 鸿蒙Next 调用convertKey生成密钥后返回内容unknown的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


使用方法可以参考如下代码:

//AES加解密
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';

@Component
export struct enCode_AES {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text('加解密')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            main();
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

function generateRandom(len: number) {
  let rand = cryptoFramework.createRandom();
  let generateRandSync = rand.generateRandomSync(len);
  return generateRandSync;
}

function genGcmParamsSpec() {
  let ivBlob = generateRandom(12); // 12 bytes
  let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes
  let dataAad = new Uint8Array(arr);
  let aadBlob: cryptoFramework.DataBlob = { data: dataAad };
  arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes
  let dataTag = new Uint8Array(arr);
  let tagBlob: cryptoFramework.DataBlob = {
    data: dataTag
  };
  // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中
  let gcmParamsSpec: cryptoFramework.GcmParamsSpec = {
    iv: ivBlob,
    aad: aadBlob,
    authTag: tagBlob,
    algName: "GcmParamsSpec"
  };
  return gcmParamsSpec;
}

let gcmParams = genGcmParamsSpec();

// 加密消息
async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('SM4_128|GCM|PKCS7');
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams);
  let encryptUpdate = await cipher.update(plainText);
  // gcm模式加密doFinal时传入空,获得tag数据,并更新至gcmParams对象中。
  gcmParams.authTag = await cipher.doFinal(null);
  return encryptUpdate;
}
// 解密消息
async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
  let decoder = cryptoFramework.createCipher('SM4_128|GCM|PKCS7');
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams);
  let decryptUpdate = await decoder.update(cipherText);
  // gcm模式解密doFinal时传入空,验证init时传入的tag数据,如果验证失败会抛出异常。
  let decryptData = await decoder.doFinal(null);
  if (decryptData == null) {
    console.info('GCM decrypt success, decryptData is null');
  }
  return decryptUpdate;
}
async function genSymKeyByData(symKeyData: Uint8Array) {
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
  let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4_128');
  let symKey = await sm4Generator.convertKey(symKeyBlob);
  console.info('convertKey success');
  return symKey;
}
async function main() {
  let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]);
  let symKey = await genSymKeyByData(keyData);
  let message = "This is a test";
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
  let encryptText = await encryptMessagePromise(symKey, plainText);
  let decryptText = await decryptMessagePromise(symKey, encryptText);
  if (plainText.data.toString() === decryptText.data.toString()) {
    console.info('decrypt ok');
    console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
  } else {
    console.error('decrypt failed');
  }
}

参考网址:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-sm4-sym-encrypt-decrypt-gcm-V5

针对帖子标题“HarmonyOS 鸿蒙Next 调用convertKey生成密钥后返回内容unknown”的问题,在HarmonyOS鸿蒙Next系统中,调用convertKey函数生成密钥后返回“unknown”内容,这通常指示函数执行过程中遇到了预期之外的情况或错误。可能的原因包括但不限于:

  1. 输入参数不正确:确保传递给convertKey函数的参数符合其要求的格式和类型。

  2. 系统资源限制:检查系统是否有足够的资源(如内存、存储空间)来执行密钥生成操作。

  3. 权限问题:确保应用程序具有执行密钥生成操作所需的权限。

  4. 底层库或API问题:可能是HarmonyOS的底层库或API存在bug或未完全支持该功能。

  5. 特定环境或配置问题:检查是否在特定的设备、系统版本或配置下出现了此问题。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部