aesCbcEncrypt function
AES-256-CBC encrypt. Random 16-byte IV prepended to ciphertext. Matches Python's iv_scramble.
Implementation
Uint8List aesCbcEncrypt(Uint8List plaintext, Uint8List key) {
final rng = Random.secure();
final iv = Uint8List.fromList(List.generate(16, (_) => rng.nextInt(256)));
final padded = _pkcs7Pad(plaintext);
final cipher = CBCBlockCipher(AESEngine())
..init(true, ParametersWithIV(KeyParameter(key), iv));
final out = Uint8List(padded.length);
for (var offset = 0; offset < padded.length; offset += 16) {
cipher.processBlock(padded, offset, out, offset);
}
// IV + ciphertext
final result = Uint8List(16 + out.length);
result.setAll(0, iv);
result.setAll(16, out);
return result;
}