This commit is contained in:
spasolreisa
2026-04-16 14:26:52 +08:00
commit 9ce601aa8d
151 changed files with 11467 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:encrypt/encrypt.dart' as enc;
import 'package:archive/archive.dart';
class EncryptionUtil {
// 固定密钥(和后端保持一致)
static const String _keyStr = ",Lscj312.;[]sc`1dsajcjc;;wislacx";
static const String _ivStr = ",>ew:[7890;,wd[2";
// 对外简易方法(你 Service / Provider 里直接用)
static String encrypt(String data) => encryptAndCompress(data);
static String decrypt(String encryptedBase64) => decompressAndDecrypt(encryptedBase64);
/// 加密:字符串 → GZIP → AES → Base64
static String encryptAndCompress(String plainText) {
// 强制 32位 Key / 16位 IVAES 标准)
final key = enc.Key.fromUtf8(_keyStr.padRight(32).substring(0, 32));
final iv = enc.IV.fromUtf8(_ivStr.padRight(16).substring(0, 16));
final encrypter = enc.Encrypter(enc.AES(key, mode: enc.AESMode.cbc));
// GZIP 压缩
final utf8Bytes = utf8.encode(plainText);
final compressed = GZipEncoder().encode(utf8Bytes)!;
// AES 加密
final encrypted = encrypter.encryptBytes(
Uint8List.fromList(compressed),
iv: iv,
);
return encrypted.base64;
}
/// 解密Base64 → AES → GZIP → 字符串
static String decompressAndDecrypt(String encryptedBase64) {
final key = enc.Key.fromUtf8(_keyStr.padRight(32).substring(0, 32));
final iv = enc.IV.fromUtf8(_ivStr.padRight(16).substring(0, 16));
final encrypter = enc.Encrypter(enc.AES(key, mode: enc.AESMode.cbc));
// 解密
final encrypted = enc.Encrypted.fromBase64(encryptedBase64);
final decryptedBytes = encrypter.decryptBytes(encrypted, iv: iv);
// GZIP 解压
final decompressed = GZipDecoder().decodeBytes(decryptedBytes);
return utf8.decode(decompressed);
}
}