package com.seer.teach.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;

/**
 * @author Captain
 */
@Slf4j
@Component
public class AESUtils {
    private static final String IV_STRING = "2a3b4c5d6e7f8a9b";

    private static final String PASSWORD = "9c5d3a2b1e0f8d7c";

    private static final String encoding = "UTF-8";

    private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
    private static final int BUFFER_SIZE = 8192;

    /**
     * aes加密
     * @param content 加密内容
     * @return
     */

    public static String encrypt(String content) {
        return encryptAES(content, PASSWORD);
    }

    /**
     * aes解密
     * @param content aes加密的内容
     * @return
     */
    public static String decrypt(String content) {
        return decryptAES(content, PASSWORD);
    }



    public static String encryptAES(String content, String key) {
        try {
            byte[] byteContent = content.getBytes(encoding);
            byte[] enCodeFormat = key.getBytes(encoding);
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
            byte[] initParam = IV_STRING.getBytes(encoding);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            byte[] encryptedBytes = cipher.doFinal(byteContent);
            String base64 = Base64.getEncoder().encodeToString(encryptedBytes);
            return URLEncoder.encode(base64, encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public static String decryptAES(String content, String key) {
        try {
            //URL解码
            content = URLDecoder.decode(content, encoding);
//            byte[] encryptedBytes = Base64.decode(content, Base64.DEFAULT);
            byte[] encryptedBytes = Base64.getDecoder().decode(content);
            byte[] enCodeFormat = key.getBytes(encoding);
            SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
            byte[] initParam = IV_STRING.getBytes(encoding);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] result = cipher.doFinal(encryptedBytes);
            return new String(result, encoding);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("AES解密异常", e);
        }
        return null;
    }

    public static void main(String[] args) {
        String content = "xBVUfvzfPw7pVIVxBcLIAP0HbAhgIMA3fmO9GKtpzv8";
        System.out.println("decrypt = " + decryptAES(content, PASSWORD));
    }



}