encrypt static method

String encrypt(
  1. String data,
  2. String keyString, {
  3. String? ivString,
  4. AESMode mode = encriptor.AESMode.sic,
})

Encrypts the given data using AES encryption with the provided keyString.

The keyString must be 32 characters long. The optional ivString must be 32 characters long. If ivString is not provided, a part of keyString will be used as the initialization vector (IV).

Returns a Base64 encoded string of the encrypted data.

Throws an ArgumentError if keyString is not 32 characters long.

Implementation

static String encrypt(
  String data,
  String keyString, {
  String? ivString,
  encriptor.AESMode mode = encriptor.AESMode.sic,
}) {
  if (data.isEmpty) {
    return '';
  } else if (keyString.length < 32) {
    return 'keyString length must be 32 characters.';
  }
  final key = encriptor.Key.fromUtf8(keyString.substring(0, 32));
  final iv = encriptor.IV.fromUtf8(ivString ?? keyString.substring(0, 16));
  final encrypter = encriptor.Encrypter(encriptor.AES(key, mode: mode));
  final encryptedData = encrypter.encrypt(data, iv: iv);
  return encryptedData.base64;
}