package com.testor.common.util;

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

public class AESEncryptionUtil {
    private static final String SECRET_KRY = "ZLMYAHPT!@#$%^&*";
    private static final String ALGORITHM = "AES";

    public static String getSecretKry() {
        return SECRET_KRY;
    }

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

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

    public static void main(String[] args) throws Exception {
        SecretKey secretKey = new SecretKeySpec(SECRET_KRY.getBytes(), "AES");
        String decrypt = decrypt("WnF8TEc4sQOySsM05NH+rA==", secretKey);
        System.out.println(decrypt);
    }
}

