AES加解密无法使用,有人知道原因吗?

AES加解密无法使用。

HarmonyOS
10h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考以下Demo进行尝试:

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
 
@Entry 
@Component 
struct Index{ 
  build() { 
    Column(){ 
      Button("加解密测试") 
        .onClick(()=>{ 
          aes() 
        }) 
    } 
    .justifyContent(FlexAlign.Center) 
    .width("100%") 
    .height("100%") 
  } 
} 
 
function genGcmParamsSpec() { 
  let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes 
  let dataIv = new Uint8Array(arr); 
  let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 
  arr = [0, 0, 0, 0, 0, 0, 0, 0]; // 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 
  }; // The GCM authTag is obtained by doFinal() in encryption and passed in params of init() in decryption. 
  let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { 
    iv: ivBlob, 
    aad: aadBlob, 
    authTag: tagBlob, 
    algName: "GcmParamsSpec" 
  }; 
  return gcmParamsSpec; 
} 
let gcmParams = genGcmParamsSpec(); 
 
// 分段加密消息 
async function encryptMessageUpdateBySegment(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 
  let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); 
  let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 
  let cipherText = new Uint8Array(); 
  for (let i = 0; i < plainText.data.length; i += updateLength) { 
    let updateMessage = plainText.data.subarray(i, i + updateLength); 
    let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 
    // 分段update 
    let updateOutput = await cipher.update(updateMessageBlob); 
    // 把update的结果拼接起来,得到密文(有些情况下还需拼接doFinal的结果,这取决于分组模式 
    // 和填充模式,本例中GCM模式的doFinal结果只包含authTag而不含密文,所以不需要拼接) 
    let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); 
    mergeText.set(cipherText); 
    mergeText.set(updateOutput.data, cipherText.length); 
    cipherText = mergeText; 
  } 
  gcmParams.authTag = await cipher.doFinal(null); 
  let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; 
  return cipherBlob; 
} 
 
// 分段解密消息 
async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 
  let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); 
  let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 
  let decryptText = new Uint8Array(); 
  for (let i = 0; i < cipherText.data.length; i += updateLength) { 
    let updateMessage = cipherText.data.subarray(i, i + updateLength); 
    let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 
    // 分段update 
    let updateOutput = await decoder.update(updateMessageBlob); 
    // 把update的结果拼接起来,得到明文 
    let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); 
    mergeText.set(decryptText); 
    mergeText.set(updateOutput.data, decryptText.length); 
    decryptText = mergeText; 
  } 
  let decryptData = await decoder.doFinal(null); 
  if (decryptData == null) { 
    console.info('GCM decrypt success, decryptData is null'); 
  } 
  let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; 
  return decryptBlob; 
} 
async function genSymKeyByData(symKeyData: Uint8Array) { 
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 
  let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 
  let symKey = await aesGenerator.convertKey(symKeyBlob); 
  console.info('convertKey success'); 
  return symKey; 
 
  async function aes() { 
    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 = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee"; // 假设信息总共43字节,根据utf-8解码后,也是43字节 
    let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 
    let encryptText = await encryptMessageUpdateBySegment(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'); 
    } 
  }
分享
微博
QQ
微信
回复
5h前
相关问题
HarmonyOS AES加解密与java无法通用
335浏览 • 1回复 待解决
导包报错,有人知道原因
946浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
444浏览 • 1回复 待解决
如何进行不同规格的AES加解密
647浏览 • 1回复 待解决
拉起收银台报错,有人知道原因
1479浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
1917浏览 • 1回复 待解决
taskpool 使用问题,有人知道
778浏览 • 1回复 待解决
$$语法如何使用有人知道
442浏览 • 1回复 待解决
HarmonyOS 有RSA加解密相关
292浏览 • 0回复 待解决
加解密算法库框架使用
762浏览 • 1回复 待解决
如何使用快速修复,有人知道
560浏览 • 1回复 待解决
ArkTS要如何使用this,有人知道
387浏览 • 1回复 待解决
HarmonyOS 加解密 demo
338浏览 • 1回复 待解决
HarmonyOS AES解密报错
433浏览 • 1回复 待解决
有人知道轻量系统上怎么使用MQTT
2224浏览 • 2回复 已解决
有人知道关于页demo
664浏览 • 1回复 待解决
加解密问题的定位指导
233浏览 • 1回复 待解决
如何使用SM4的CBC模式加解密
722浏览 • 1回复 待解决
有人知道JS menu如何隐藏
4442浏览 • 1回复 待解决