package com.chinaweal.aiccs.config;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AesUtils {
    private static final String ALGORITHM = "AES";
    private static final String SECRET_KEY = "chinaweal@202506"; // 如 "1234567890123456"

    public static String encrypt(String data) throws Exception {
        SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String encryptedData) throws Exception {
        SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decoded = Base64.getDecoder().decode(encryptedData);
        byte[] decrypted = cipher.doFinal(decoded);
        return new String(decrypted);
    }

//    public static void main(String[] args) {
//        try {
//            // 加密测试
//            System.out.println("SYSDBA 加密结果: " + AesUtils.encrypt("SYSDBA"));
//            System.out.println("chinaweal 加密结果: " + AesUtils.encrypt("chinaweal"));
//
//            String data = AesUtils.encrypt("SYSDBA");
//            String data1 = AesUtils.encrypt("chinaweal");
//            // 解密测试(验证)
//            System.out.println("解密验证 SYSDBA: " +
//                    AesUtils.decrypt(data));
//            System.out.println("解密验证 chinaweal: " +
//                    AesUtils.decrypt(data1));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
