使用AES对称密钥(CBC模式)加解密,生成symKey失败

使用官方代码进行AES解密数据,生成symKey失败。

传入参数秘钥key:f951cbcc2d3d8f4de802744d8fe6103d

传入参数iv:563b7a6e46394561b9cd6e93564f1632

代码如下:

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
 
 
/** 
 * AES解密方法 
 * @param key 秘钥 
 * @param iv 
 * @param data 待解密数据 
 * @returns 
 */ 
export async function aes128CBC_decrypt(key:string,iv:string,data:ArrayBuffer):Promise<cryptoFramework.DataBlob> { 
  let keyData = new Uint8Array(buffer.from(key, 'utf-8').buffer); 
  // let keyData = stringToUint8Array(key); 
  let length = key.length; 
  let symKey = await genSymKeyByData(keyData); 
  let decryptData: cryptoFramework.DataBlob = { data: new Uint8Array(data) }; 
  let decryptDataSuccess = await decryptMessagePromise(symKey,iv,decryptData); 
  return decryptDataSuccess; 
} 
 
//生成CBC模式的ivParamsSpec 
function genIvParamsSpec(iv:string) { 
  let dataIv = stringToUint8Array(iv); 
  let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 
  let ivParamsSpec: cryptoFramework.IvParamsSpec = { 
    algName: "IvParamsSpec", 
    iv: ivBlob 
  }; 
  return ivParamsSpec; 
} 
//生成密钥算法为AES、密钥长度为128位的对称密钥(SymKey) 
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 decryptMessagePromise(symKey: cryptoFramework.SymKey,iv:string, cipherText: cryptoFramework.DataBlob) { 
  let decoder = cryptoFramework.createCipher('AES128|CBC|PKCS7'); 
  let ivParamsSpec = genIvParamsSpec(iv); 
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, ivParamsSpec); 
  let decryptData = await decoder.doFinal(cipherText); 
  return decryptData; 
} 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
HarmonyOS
2024-11-08 11:50:16
2719浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

​使用AES对称密钥(CBC模式)加解密

可以参考以下链接:​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-aes-sym-encrypt-decrypt-cbc-V5

参考demo:​

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
 
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('AES128|CBC|PKCS7'); 
  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('AES128|CBC|PKCS7'); 
  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('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('decrypt ok'); 
      console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 
    } else { 
      console.error('decrypt failed'); 
    } 
  } catch (error) { 
    console.error(`AES CBC “${error}“, error code: ${error.code}`); 
  } 
} 
 
 
@Entry 
@Component 
struct Page { 
  @State message: string = 'Hello World'; 
 
  build() { 
    RelativeContainer() { 
      Text(this.message) 
        .id('PageHelloWorld') 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .alignRules({ 
          center: { anchor: '__container__', align: VerticalAlign.Center }, 
          middle: { anchor: '__container__', align: HorizontalAlign.Center } 
        }) 
        .onClick(async () => { 
 
          await main() 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
分享
微博
QQ
微信
回复
2024-11-08 15:10:36


相关问题
HarmonyOS AES-CBC加解密
473浏览 • 1回复 待解决
如何使用SM4的CBC模式加解密
1615浏览 • 1回复 待解决
SM4 CBC模式加解密,有好的方案吗?
1470浏览 • 1回复 待解决
HarmonyOS AES加解密问题
579浏览 • 1回复 待解决
HarmonyOS AES加解密咨询
584浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
1581浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
1204浏览 • 1回复 待解决
HarmonyOS AES加解密报错17630001
494浏览 • 1回复 待解决
HarmonyOS AES128/CBC/NoPadding加密模式报错
355浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
881浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
517浏览 • 1回复 待解决
如何进行不同规格的AES加解密
1309浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
267浏览 • 1回复 待解决