decrypt method

List<int> decrypt(
  1. String value
)

Decrypts the encrypted string. This supports having a pre-set IV or having the IV encoded on the value by having the value encoded as ${base64Iv}:${base64EncryptedValue}.

Implementation

List<int> decrypt(String value) {
  var iv = _iv;
  var encrypted = value;
  if (iv == null || value.contains(':')) {
    final parts = value.split(':');

    if (parts.length != 2) {
      throw Exception('Attempted to AES decrypt but no IV has been set.');
    }

    iv ??= IV.fromBase64(parts[0]);
    encrypted = parts[1];
  }

  final key = _key;

  if (key == null) {
    throw Exception('Attempted to AES decrypt but no key has been set.');
  }

  final encrypter = Encrypter(
    AES(
      Key(key),
      mode: _mode,
      padding: _padding,
    ),
  );

  final result = encrypter.decryptBytes(
    Encrypted(base64.decode(encrypted)),
    iv: iv,
  );

  return result;
}