aesCbcEncrypt static method
Implementation
static Uint8List aesCbcEncrypt(
Uint8List key,
Uint8List iv,
String text,
) {
final paddedPlaintext = pad(utf8.encode(text) as Uint8List, 16);
if (![128, 192, 256].contains(key.length * 8)) {
throw ArgumentError.value(key, 'key', 'invalid key length for AES');
}
if (iv.length * 8 != 128) {
throw ArgumentError.value(iv, 'iv', 'invalid IV length for AES');
}
if (paddedPlaintext.length * 8 % 128 != 0) {
throw ArgumentError.value(
paddedPlaintext, 'paddedPlaintext', 'invalid length for AES');
}
// Create a CBC block cipher with AES, and initialize with key and IV
final cbc = BlockCipher('AES/CBC')
..init(true, ParametersWithIV(KeyParameter(key), iv)); // true=encrypt
// Encrypt the plaintext block-by-block
final cipherText = Uint8List(paddedPlaintext.length); // allocate space
var offset = 0;
while (offset < paddedPlaintext.length) {
offset += cbc.processBlock(paddedPlaintext, offset, cipherText, offset);
}
assert(offset == paddedPlaintext.length);
return cipherText;
}