HarmonyOS 对称加密不支持AES|CBC|NoPadding模式

1、文档描述API10后,支持对称加解密不带密钥长度的规格,测试发现不支持createSymKeyGenerator('AES')写法。

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-sym-encrypt-decrypt-spec-V5

2、对称加密不支持AES|CBC|NoPadding模式

function genIvParamsSpec() {
  let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 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('AES|CBC|NoPadding');
  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('AES|CBC|NoPadding');
  let iv = genIvParamsSpec();
  decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv);
  let decryptData = decoder.doFinalSync(cipherText);
  return decryptData;
}

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

async function main() {
  try {
    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 = encryptMessage(symKey, plainText);
    let decryptText = decryptMessage(symKey, encryptText);
    if (plainText.data.toString() === decryptText.data.toString()) {
      console.info('cwq decrypt ok');
      console.info('cwq decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
    } else {
      console.error('cwq decrypt failed');
    }
  } catch (error) {
    console.error(`cwq AES CBC “${error}“, error code: ${error.code}`);
  }
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

使用NoPadding密钥和明文长度一致啊,有限制的,可以用PKCS5、PKCS7就行

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS AES128|CBC|NoPadding 加密报错
58浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
607浏览 • 1回复 待解决
RSA NoPadding模式加密失败
1271浏览 • 1回复 待解决
HarmonyOS SM4-ECB-NoPadding加密
61浏览 • 1回复 待解决
HarmonyOS Span不支持n换行
36浏览 • 1回复 待解决
HarmonyOS image不支持mask吗
52浏览 • 1回复 待解决
web组件不支持localstorage
837浏览 • 1回复 待解决
是否有AES的ECB模式加密算法
579浏览 • 1回复 待解决
HarmonyOS AES加密
35浏览 • 1回复 待解决
AES gcm模式加密有对应的库吗?
243浏览 • 1回复 待解决
HarmonyOS Object不支持 ... 展开符吗?
288浏览 • 1回复 待解决
HarmonyOS ets不支持匿名类吗?
243浏览 • 2回复 待解决
HarmonyOS Text的Span不支持align
28浏览 • 1回复 待解决