HarmonyOS 鸿蒙Next AES加密aesGenerator.convertKey报错code:401 convert sym key failed

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

HarmonyOS 鸿蒙Next AES加密aesGenerator.convertKey报错code:401 convert sym key failed

AES加密aesGenerator.convertKey报错code:401 convert sym key failed 
let symKeyBlob: cryptoFramework.DataBlob = { data: key };
let aesGenerator = cryptoFramework.createSymKeyGenerator(“AES128”);
let symKey = await aesGenerator.convertKey(symKeyBlob);
调用await aesGenerator.convertKey(symKeyBlob)方法报错 code:401 convert sym key failed


更多关于HarmonyOS 鸿蒙Next AES加密aesGenerator.convertKey报错code:401 convert sym key failed的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

报错为401 入参错误 因为需要对密钥进行转换 参考以下加解密的代码demo实例

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

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


@Entry
@Component
struct AESCBCCrypto {
  @State message: string = '点击开始';

  build() {

    Row() {

      Column() {

        Text(this.message)

          .fontSize(50)

          .fontWeight(FontWeight.Bold)

          .onClick(() => {

            aesCBC()

          })

      }

      .width('100%')

    }

    .height('100%')

  }
}

function genIvParamsSpec() {

  // let iv = new Uint8Array(buffer.from('0fa849f99a7b4738', 'utf-8').buffer);

  // let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes

  let arr = [-45, -21, 108, 107, 31, 7, 62, 12, 51, -39, 87, 89, -18, 48, 3, 23]; // 16 bytes

  let dataIv = new Uint8Array(arr);

  let ivBlob: cryptoFramework.DataBlob = { data: dataIv };

  let ivParamsSpec: cryptoFramework.IvParamsSpec = {

    algName: "IvParamsSpec",

    iv: ivBlob

  };

  return ivParamsSpec;

}

// 加密消息

function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {

  let cipher = cryptoFramework.createCipher('AES256|CBC|PKCS5');

  let iv = genIvParamsSpec();

  cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv);

  let cipherData = cipher.doFinalSync(plainText);

  return cipherData;

}

// 解密消息

function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {

  let decoder = cryptoFramework.createCipher('AES256|CBC|PKCS5');

  let iv = genIvParamsSpec();

  decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv);

  let decryptData = decoder.doFinalSync(cipherText);

  return decryptData;

}

function genSymKeyByData(symKeyData: Uint8Array) {

  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };

  let aesGenerator = cryptoFramework.createSymKeyGenerator('AES256');

  let symKey = aesGenerator.convertKeySync(symKeyBlob);

  console.info('convertKey success');

  return symKey;

}

function aesCBC() {

  try {

    let base64 = new util.Base64Helper();


    let pk = "1234567891234567";

    // let pk = "{\"anwser\":51.0,\"content\":\"x - 15 = 36\\nx = ?\",\"image\":\"\",\"index\":3,\"subject\":\"\",\"tips\":\"\"},{\"anwser\":9.0,\"content\":\"1, 3, 5, 7, ?\",\"image\":\"\",\"index\":4,\"subject\":\"\",\"tips\":\"后一个数比前一个数+2\"}";;

    // let pk = 'bmV3YWlyY2xvdWRfdmpvdzlEZWojSkRqNFtvSURG';

    // let key = base64.decodeSync(pk);

    // let key = new Uint8Array(buffer.from(pk, 'utf8').buffer);

    let key =
      new Uint8Array([56, 53, 101, 57, 49, 53, 56, 99, 52, 97, 50, 53, 99, 55, 48, 97, 55, 54, 49, 101, 57, 102, 100,
        56, 48, 51, 48, 102, 98, 101, 56, 52]);

    // let iv = new Uint8Array(buffer.from('0102030405060708', 'utf-8').buffer);

    // let base64pk = base64.decodeSync(pk);

    let symKey = genSymKeyByData(key);

    // let message = `{"anwser":51.0,"content":"x - 15 = 36\\nx = ?","image":"","index":3,"subject":"","tips":""},{"anwser":9.0,"content":"1, 3, 5, 7, ?","image":"","index":4,"subject":"","tips":"后一个数比前一个数+2"}`;

    let messageDecrypto = "9elNUOxN3BGsBsqHFSkJ5cd+jBy/Tvhlq/QFWJ2VhUQ=";

    // let base64message = base64.decodeSync(message);

    // buffer.from(basemessage,'hex')

    // let newBuf = buffer.from(buffer.from(message, 'utf-8').buffer);

    let text = new Uint8Array([1, 3, 4, 102, -102, 18, -32, -6])

    // let message2base64 = "d23b4a05fe84c64a9bcb061dc5c8930c";

    let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(text) };

    let encryptText = encryptMessage(symKey, plainText);

    // console.error('encrypt 16进制 plainText: ' + buffer.from(encryptText.data).toString('hex'));

    console.error('encrypt base64 plainText: ' + base64.encodeToStringSync(encryptText.data));

    // console.error('decrypt plainText: ' + buffer.from(encryptText.data).toString('hex'));

    // buffer.from(encryptText.data.toString(), 'hex').buffer

    let decryptText = decryptMessage(symKey, encryptText);

    console.error('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));

    if (plainText.data.toString() === decryptText.data.toString()) {

      console.error('decrypt ok' + plainText.data);

      // console.error('decrypt plainText: ' + base64.encodeToStringSync(decryptText.data));

      // console.error('decrypt plainText: ' + buffer.from(base64.encodeToStringSync(decryptText.data),'utf-8').buffer);

    } else {

      console.error('decrypt failed');

    }

  } catch (error) {

    console.error(JSON.stringify(error));

  }

}

更多关于HarmonyOS 鸿蒙Next AES加密aesGenerator.convertKey报错code:401 convert sym key failed的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,AES加密报错code:401 convert sym key failed通常指的是在密钥转换过程中遇到了问题。此错误可能由以下几个原因引起:

  1. 密钥格式不正确:确保你提供的密钥符合AES加密所需的格式和长度要求。对于AES-128,密钥应为16字节;AES-192为24字节;AES-256为32字节。

  2. 密钥类型不匹配:检查你尝试转换的密钥类型是否与加密算法要求的密钥类型一致。例如,某些API可能要求密钥为原始字节数组,而不是经过编码的字符串。

  3. API使用错误:确认你使用的API调用是正确的,且所有必要的参数都已正确设置。检查API文档以确认没有遗漏或错误使用任何参数。

  4. 权限问题:在某些情况下,权限不足也可能导致密钥转换失败。确保你的应用具有执行加密操作所需的权限。

  5. 系统或库的问题:如果以上均无误,可能是系统或加密库本身的bug。尝试更新系统或库到最新版本。

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

回到顶部