The AES class implements the Algorithm interface for symmetric encryption using the Advanced Encryption Standard. It requires a Key and supports various modes and padding schemes.
Constructor
AES(this.key, {this.mode = AESMode.sic, this.padding = 'PKCS7'})
key: An instance of Key containing the secret bytes.mode: An AESMode value (defaults to AESMode.sic).padding: A string specifying the padding scheme (defaults to 'PKCS7'). If set to null, no padding is applied.
Methods
encrypt
Encrypted encrypt(Uint8List bytes, {IV? iv, Uint8List? associatedData})
Encrypts the provided Uint8List bytes.
- Note: If the mode is not
AESMode.ecb, an IV (Initialization Vector) must be provided, otherwise a StateError('IV is required.') is thrown. associatedData: Optional data used for authenticated encryption modes like AESMode.gcm.
decrypt
Uint8List decrypt(Encrypted encrypted, {IV? iv, Uint8List? associatedData})
Decrypts the provided Encrypted object.
- Note: If the mode is not
AESMode.ecb, an IV must be provided, otherwise a StateError('IV is required.') is thrown. associatedData: Optional data used for authenticated encryption modes like AESMode.gcm.
// Example usage of AES encryption
final key = Key.fromBytes(myBytes);
final aes = AES(key, mode: AESMode.cbc, padding: 'PKCS7');
final iv = IV.fromBytes(myIvBytes);
final encrypted = aes.encrypt(dataBytes, iv: iv);
final decrypted = aes.decrypt(encrypted, iv: iv);