import 'dart:convert';

import 'package:encrypt/encrypt.dart' as Encrypt;
import 'package:encrypt/encrypt.dart';
import 'package:flutter_wallet/tools/YSTools.dart';
import 'package:hex/hex.dart';
const  String _key = '4d7d36f1a720586187281ecbabfcb99f33961897ccd349484e12920c320fcc01';
const  String _iv = 'dd25f06a4332857721633b00475ee1a0';

class YSAes{
  ///aes加密函数
  static String aesEncode3(String content){
    LogUtil.d('aesEncode=========$content');

    // 将十六进制字符串转换为字节序列
    List<int> keyBytes = HEX.decode(_key);
    List<int> ivBytes = HEX.decode(_iv);

    // 将字节序列转换为 Base64 字符串
    String keyBase64 = base64.encode(keyBytes);
    String ivBase64 = base64.encode(ivBytes);

    // 使用 Base64 字符串创建密钥和 IV
    final key = Key.fromBase64(keyBase64);
    final iv = IV.fromBase64(ivBase64);

    final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
    final encrypted = encrypter.encrypt(content, iv: iv);
    final decrypted = encrypter.decrypt(encrypted, iv: iv);

    print(decrypted); // 显示：Hello, World!
    print(encrypted.base64); // 显示加密后的数据
    return encrypted.base64;

  }
  /// 解密函数
  static String aesDecrypted(String data){
    //加密key
    final key = Encrypt.Key.fromUtf8(_key);
    //偏移量
    final iv = Encrypt.IV.fromUtf8(_iv);

    //设置cbc模式
    final encrypter = Encrypt.Encrypter(
        Encrypt.AES(key, mode: Encrypt.AESMode.cbc, padding: 'PKCS7'));
    //解密
    LogUtil.d( "解密后的结果:${encrypter.decrypt(Encrypted.fromBase64(data), iv: iv)}");
    return encrypter.decrypt(Encrypted.fromBase64(data), iv: iv);

  }
}