Files
ln-ios/ln_jq_app/lib/common/login_util.dart
2025-11-05 10:10:47 +08:00

38 lines
1.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:encrypt/encrypt.dart';
class LoginUtil {
// 定义密钥
// 对于ECB模式我们只需要Key不需要IV (初始化向量)
static final _keyString = '915eae87951a448c86c47796e44c1fcf';
static final _key = Key.fromUtf8(_keyString);
// 【核心修改】创建使用 ECB 模式的加密器实例
// 1. mode: AESMode.ecb -> 使用ECB模式它不需要IV。
// 2. padding: 'PKCS7' -> 在encrypt库中PKCS5和PKCS7的填充方式是兼容的。
static final _encrypter = Encrypter(AES(_key, mode: AESMode.ecb, padding: 'PKCS7'));
/// AES 加密方法
static String encrypt(String plainText) {
if (plainText.isEmpty) {
return '';
}
// 【核心修改】调用 encrypt 方法时不再需要传递 iv
final encrypted = _encrypter.encrypt(plainText);
// 返回Base64编码的密文这是网络传输的标准做法
return encrypted.base64;
}
/// AES 解密方法 (可选,如果需要解密的话)
static String decrypt(String encryptedText) {
if (encryptedText.isEmpty) {
return '';
}
final encrypted = Encrypted.fromBase64(encryptedText);
// 【核心修改】调用 decrypt 方法时不再需要传递 iv
final decrypted = _encrypter.decrypt(encrypted);
return decrypted;
}
}