aes_util.dart 695 B

1234567891011121314151617181920212223242526
  1. import 'package:encrypt/encrypt.dart' as en;
  2. /// AES加密工具类
  3. class AESUtil {
  4. /// 单例模式
  5. static final AESUtil _instance = AESUtil._internal();
  6. AESUtil._internal();
  7. factory AESUtil() => _instance;
  8. final _key = en.Key.fromUtf8('Og6k#Fv@H9HSELVVgQzFuQVWmQ8^FfMQ');
  9. final _iv = en.IV.fromLength(16);
  10. /// AES加密
  11. String encrypt(String data) {
  12. final encrypter = en.Encrypter(en.AES(_key));
  13. final encrypted = encrypter.encrypt(data, iv: _iv);
  14. return encrypted.base64;
  15. }
  16. /// AES解密
  17. String decrypt(String data) {
  18. final encrypter = en.Encrypter(en.AES(_key));
  19. final decrypted = encrypter.decrypt64(data, iv: _iv);
  20. return decrypted;
  21. }
  22. }