qhd-prevention-flutter/lib/tools/AESEncryption.dart

32 lines
842 B
Dart
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'dart:convert';
import 'package:encrypt/encrypt.dart' as enc;
/// AES/ECB/PKCS5Padding 加密
String aesEncryptEcbPkcs5({
required String plainText,
String keyUtf8="daac3ae52eff4cec",
}) {
final key = enc.Key.fromUtf8(keyUtf8);
final encrypter = enc.Encrypter(enc.AES(
key,
mode: enc.AESMode.ecb,
padding: 'PKCS7', // Dart里用PKCS7对齐Java的PKCS5
));
final encrypted = encrypter.encrypt(plainText);
return encrypted.base64;
}
/// AES/ECB/PKCS5Padding 解密
String aesDecryptEcbPkcs5({
required String cipherBase64,
required String keyUtf8,
}) {
final key = enc.Key.fromUtf8(keyUtf8);
final encrypter = enc.Encrypter(enc.AES(
key,
mode: enc.AESMode.ecb,
padding: 'PKCS7',
));
final decrypted = encrypter.decrypt(enc.Encrypted.fromBase64(cipherBase64));
return decrypted;
}