decryptBytes method

  1. @override
Future<Uint8List> decryptBytes({
  1. required Uint8List cipherBytes,
  2. required String key,
  3. String? iv,
})
override

Decrypts cipherBytes (format: [16-byte IV][ciphertext]) and returns the original plaintext.

When iv is provided it is used as the IV and the first 16 bytes of cipherBytes are still skipped (they were written by encryptBytes).

Implementation

@override
Future<Uint8List> decryptBytes({
  required Uint8List cipherBytes,
  required String key,
  String? iv,
}) async {
  try {
    final keyBytes = prepareKey(key);
    final Uint8List ivBytes;
    final Uint8List data;
    if (iv != null) {
      ivBytes = prepareIv(iv);
      data = cipherBytes.sublist(16);
    } else {
      ivBytes = cipherBytes.sublist(0, 16);
      data = cipherBytes.sublist(16);
    }
    return aesCtrCrypt(data, keyBytes, ivBytes);
  } catch (e, st) {
    dev.log('decryptBytes failed: $e', name: 'MethodChannelAesEncryptFile', error: e, stackTrace: st);
    rethrow;
  }
}