- Install Encrypt package from https://pub.dev/packages/encrypt
- Import your package import 'package:encrypt/encrypt.dart' as enc;
- Use the below code for encryption and decryption
- Point to remember key length should be either 32 lengths,64 length and 128 length
void encryption(String text) {
final key = enc.Key.fromUtf8('my 32 length key................');
final iv = enc.IV.fromLength(1);
final encrypter = enc.Encrypter(AES(key));
final encrypted = encrypter.encrypt(text, iv: iv);
setState(() {
encryptedText = encrypted.base64;
});
}
void decrypt(String text) {
final key = enc.Key.fromUtf8('my 32 length key................');
final iv = enc.IV.fromLength(1);
final encrypter = enc.Encrypter(AES(key));
final decrypted = encrypter.decrypt64(text, iv: iv);
setState(() {
decryptedText = decrypted;
});
}