This commit is contained in:
2022-10-20 17:39:17 +08:00
parent 0a81762ba1
commit 30a9279ff1
8 changed files with 134 additions and 18 deletions

View File

@@ -0,0 +1,28 @@
import 'dart:convert';
import 'package:encrypt/encrypt.dart';
import 'package:crypto/crypto.dart';
class CryptoHelper {
static IV iv = IV.fromLength(16);
/// 初始化加密
static Encrypter initEncrypter(String password) {
final List<int> bytes = utf8.encode(password);
final Digest md5Bytes = md5.convert(bytes);
final Key key = Key.fromUtf8(md5Bytes.toString());
return Encrypter(AES(key, mode: AESMode.ecb));
}
/// AES 加密
static String encryptAes(String plainText, String password) {
Encrypter encrypter = CryptoHelper.initEncrypter(password);
return encrypter.encrypt(plainText, iv: iv).base64;
}
/// AES 解密
static String decryptAes(String encrypted, String password) {
Encrypter encrypter = CryptoHelper.initEncrypter(password);
return encrypter.decrypt16(encrypted, iv: iv);
}
}