aesEncrypt function

String aesEncrypt({
  1. required String text,
  2. required String mode,
  3. required String key,
  4. required String iv,
  5. Uint8List? random16Key,
})

Implementation

String aesEncrypt(
    {required String text,
    required String mode,
    required String key,
    required String iv,
    Uint8List? random16Key}) {
  String encryptData;
  if (mode == 'cbc' && random16Key != null) {
    final pKey = Key.fromUtf8(key);
    final pIv = IV.fromUtf8(iv);
    final encrypter = Encrypter(AES(pKey, mode: AESMode.cbc));
    final encrypted = encrypter.encrypt(text, iv: pIv);

    final key16 = Key(random16Key);
    final encrypterBody = Encrypter(AES(key16, mode: AESMode.cbc));
    encryptData = Uri.encodeQueryComponent(encrypterBody.encrypt(encrypted.base64, iv: pIv).base64);
  } else {
    encryptData = Uri.encodeComponent(Encrypter(AES(Key.fromUtf8(key), mode: AESMode.ecb))
        .encrypt(text, iv: IV.fromLength(0))
        .base16
        .toUpperCase());
  }
  return encryptData;
}