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

key的字符串为MD5加密后的0e23017d5e883087552887d0bc8cbae4

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
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

您使用的32位位aes256,请将128改成256 以下为代码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));
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS AES128|CBC|NoPadding 加密报错
58浏览 • 1回复 待解决
HarmonyOS AES加密
34浏览 • 1回复 待解决
HarmonyOS AES加密相关
36浏览 • 1回复 待解决
HarmonyOS RAS加密AES加密和解密
67浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
607浏览 • 1回复 待解决
HarmonyOS AES加密编码问题
83浏览 • 1回复 待解决
HarmonyOS AES加密算法demo
38浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
222浏览 • 1回复 待解决