aesEncrypt function

Future<String> aesEncrypt({
  1. required String plainText,
  2. required String secretKey,
})

Implementation

Future<String> aesEncrypt(
    {required String plainText, required String secretKey}) async {
  try {
    final salt = genRandomWithNonZero(8);
    var keyndIV = deriveKeyAndIV(secretKey, salt);
    final key = encrypt.Key(keyndIV.first);
    final iv = encrypt.IV(keyndIV.last);

    final encrypter = encrypt.Encrypter(
        encrypt.AES(key, mode: encrypt.AESMode.cbc, padding: "PKCS7"));
    final encrypted = encrypter.encrypt(plainText, iv: iv);
    Uint8List encryptedBytesWithSalt = Uint8List.fromList(
        createUint8ListFromString("Salted__") + salt + encrypted.bytes);
    return base64.encode(encryptedBytesWithSalt);
  } catch (error) {
    rethrow;
  }
}