HarmonyOS AES加密,cbc,PKCS5Padding使用string的key,能否提供示例代码

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

参考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 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|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('AES128|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('AES128');
  let symKey = aesGenerator.convertKeySync(symKeyBlob);
  console.info('convertKey success');
  return symKey;
}

function aesCBC() {
  try {
    let base64 = new util.Base64Helper();

    // let pk1 = '5386ed6345b7fe22';
    let pk = 'V2hoODJHdFcvRVZqQmtEOA==';

    let key = base64.decodeSync(pk);
    // let key = new Uint8Array(buffer.from(pk1, 'hex').buffer);

    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 newBuf = buffer.from(buffer.from(message, 'utf-8').buffer);
    let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(newBuf.buffer) };

    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));
  }
}

随机生成密钥请参考以下代码demo实例

import { cryptoFramework } from '@kit.CryptoArchitectureKit';

function testSyncGenerateAesKey() {
  // 创建SymKeyGenerator实例
  let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128');
  // 使用密钥生成器随机生成对称密钥
  let promiseSymKey = symKeyGenerator.generateSymKeySync();
  // 获取对称密钥的二进制数据,输出128位密钥
  let encodedKey = promiseSymKey.getEncoded();
  console.info('key hex:' + encodedKey.data);
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 加解密方式 AES/ECB/PKCS5Padding
60浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
614浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
29浏览 • 1回复 待解决
HarmonyOS AES128|CBC|NoPadding 加密报错
64浏览 • 1回复 待解决
HarmonyOS 能否提供扫码demo代码
40浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
226浏览 • 1回复 待解决
HarmonyOS AES加密
48浏览 • 1回复 待解决