aesCbcDecrypt function

Uint8List aesCbcDecrypt(
  1. Uint8List data,
  2. Uint8List key
)

AES-256-CBC decrypt. Expects IV prepended (first 16 bytes). Matches Python's iv_unscramble.

Implementation

Uint8List aesCbcDecrypt(Uint8List data, Uint8List key) {
  if (data.length < 32) throw FormatException('Data too short for AES-CBC');
  final iv = data.sublist(0, 16);
  final ciphertext = data.sublist(16);

  final cipher = CBCBlockCipher(AESEngine())
    ..init(false, ParametersWithIV(KeyParameter(key), iv));

  final out = Uint8List(ciphertext.length);
  for (var offset = 0; offset < ciphertext.length; offset += 16) {
    cipher.processBlock(ciphertext, offset, out, offset);
  }
  return _pkcs7Unpad(out);
}