encrypt static method
Encrypts plaintext
using AES-256-CBC with PKCS#7 padding.
encKey
must be exactly 32 bytes.
iv
must be exactly 16 bytes.
RETURNS: ciphertext bytes.
Throws InternalCryptoException if encryption fails unexpectedly.
Implementation
static Uint8List encrypt({
required Uint8List encKey,
required Uint8List iv,
required Uint8List plaintext,
}) {
_requireKeyIv(encKey, iv);
try {
final key = enc.Key(encKey);
final ivObj = enc.IV(iv);
// Explicitly request CBC + PKCS7 padding.
final aes = enc.AES(
key,
mode: enc.AESMode.cbc,
padding: 'PKCS7',
);
final encrypter = enc.Encrypter(aes);
final encrypted = encrypter.encryptBytes(plaintext, iv: ivObj);
return Uint8List.fromList(encrypted.bytes);
} catch (e, st) {
// Avoid surfacing library-specific errors.
throw InternalCryptoException(cause: e, stackTrace: st);
}
}