HarmonyOS AES加密

麻烦针对下面这段java代码给出HarmonyOS 的代码,试着找了几个但是总报错

java代码如下:

try {
  SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes("UTF-8"), "AES");
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, keySpec);
  byte[] encryptedBytes = cipher.doFinal(input.getBytes("UTF-8"));
  return Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
} catch (Exception e) {
  e.printStackTrace();
}
return "";
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

示例参考:

import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';

// 加密消息
function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('AES256|ECB|PKCS5');
  cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null);
  let cipherData = cipher.doFinalSync(plainText);
  return cipherData;
}
// 解密消息
function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
  let decoder = cryptoFramework.createCipher('AES256|ECB|PKCS5');
  decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null);
  let decryptData = decoder.doFinalSync(cipherText);
  return decryptData;
}

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

async function main() {
  try {
    let keystr = 'Bp(M+ub%pmnOns79UsevKDd]xy4WcyUO'
    // let base64Helper = new util.Base64Helper();
    let keyData = stringToUint8Array(keystr)
    // let keyData = base64Helper.decodeSync(keystr)
    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 ECB “${error}“, error code: ${error.code}`);
  }
}

@Entry
@Component
struct AESDemo {
  @State message: string = 'Hello World';
  aboutToAppear(): void {
    main()
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('AESDemoHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}

function stringToUint8Array(str: string) {
  return new Uint8Array(buffer.from(str, 'utf-8').buffer);
}

encryptText转换成base64,参考示例如下:

let base64Helper = new util.Base64Helper();
let base64Str = base64Helper.encodeToStringSync(encryptText .data);

根据密钥长度,我们这边只能使用AES256这种规格

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS AES加密相关
36浏览 • 1回复 待解决
HarmonyOS RAS加密AES加密和解密
67浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
607浏览 • 1回复 待解决
HarmonyOS AES加密编码问题
83浏览 • 1回复 待解决
HarmonyOS AES加密算法demo
41浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
222浏览 • 1回复 待解决
aes-128加密问题如何实现?
236浏览 • 1回复 待解决
AES 加密问题,有知道的吗?
190浏览 • 1回复 待解决
HarmonyOS AES128|CBC|NoPadding 加密报错
58浏览 • 1回复 待解决
HarmonyOS 关于base64和aes加密相关问题
31浏览 • 1回复 待解决
是否有AES的ECB模式的加密算法
579浏览 • 1回复 待解决
AES gcm模式的加密有对应的库吗?
243浏览 • 1回复 待解决