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 {
  final keyBytes = prepareKey(key);
  final Uint8List ivBytes;
  final Uint8List data;
  if (iv != null) {
    // Derive IV from the provided string; skip the 16-byte header that
    // was written during encryption (matching native behaviour).
    ivBytes = prepareIv(iv);
    data = cipherBytes.sublist(16);
  } else {
    // Read IV from the first 16 bytes of the payload.
    ivBytes = cipherBytes.sublist(0, 16);
    data = cipherBytes.sublist(16);
  }
  return _crypto.decryptBytes(data, keyBytes, ivBytes);
}