使用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; 
} 
HarmonyOS
2024-11-08 11:50:16
浏览
收藏 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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-11-08 15:10:36
相关问题
如何使用SM4的CBC模式加解密
998浏览 • 1回复 待解决
SM4 CBC模式加解密,有好的方案吗?
1059浏览 • 1回复 待解决
HarmonyOS AES加解密咨询
85浏览 • 1回复 待解决
HarmonyOS AES加解密问题
11浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
1101浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
813浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
518浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
17浏览 • 1回复 待解决
如何进行不同规格的AES加解密
855浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
61浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
619浏览 • 1回复 待解决
HarmonyOS 加解密方式 AES/ECB/PKCS5Padding
10浏览 • 1回复 待解决