Files
2026-09-15 15:44:13 +07:00

33 lines
1.3 KiB
Dart
Raw Permalink 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 'dart:convert';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'package:encrypt/encrypt.dart';
/// 新版本解密(项目自定义协议:12 字节 nonce + sha256 派生 key/iv + AES-CBC
String aesDecryptEx(String cipher, String key) {
// final t1 = DateTime.now();
const nonceLen = 12;
final cipherBytes = base64Decode(cipher);
final nonce = cipherBytes.sublist(0, nonceLen);
final largeShaRaw = [...utf8.encode(key), ...nonce];
final largeShaRawMid = largeShaRaw.length ~/ 2;
final msgKeyLarge = sha256.convert(largeShaRaw).bytes;
final msgKey = msgKeyLarge.sublist(8, 24);
final shaRawA = [...msgKey, ...largeShaRaw.sublist(0, largeShaRawMid)];
final sha256a = sha256.convert(shaRawA).bytes;
final shaRawB = [...largeShaRaw.sublist(largeShaRawMid), ...msgKey];
final sha256b = sha256.convert(shaRawB).bytes;
final aesKey = [...sha256a.sublist(0, 8), ...sha256b.sublist(8, 24), ...sha256a.sublist(24)];
final aesIV = [...sha256b.sublist(0, 4), ...sha256a.sublist(12, 20), ...sha256b.sublist(28)];
final encrypter = Encrypter(AES(Key(Uint8List.fromList(aesKey)), mode: AESMode.cbc));
final decrypted = encrypter.decryptBytes(Encrypted(cipherBytes.sublist(nonceLen)), iv: IV(Uint8List.fromList(aesIV)));
final text = const Utf8Decoder().convert(decrypted);
return text;
}