package org.jeecg.modules.oauth2_server.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.encryption.EncryptedString;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author liushuai
 * @create 2022-02-08 15:41
 */
public class AesUtil {
    /**
     * 加密
     *
     * @param secret
     * @param salt
     * @param data
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String secret, String salt  ) throws Exception {

        SecretKeySpec skeySpec = new SecretKeySpec(secret.getBytes(), "AES");

        IvParameterSpec iv = new IvParameterSpec(salt.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(data.getBytes("utf-8"));

        String cipherText = new String(Base64.encodeBase64(encrypted));

        return cipherText;

    }


    /**
     * 加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encrypt(String data ) throws Exception {
        String secret = EncryptedString.key;
        String salt = EncryptedString.iv;

        SecretKeySpec skeySpec = new SecretKeySpec(secret.getBytes(), "AES");

        IvParameterSpec iv = new IvParameterSpec(salt.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(data.getBytes("utf-8"));

        String cipherText = new String(Base64.encodeBase64(encrypted));

        return cipherText;

    }

    /**
     * 解密
     *
     * @param secret
     * @param salt
     * @param data
     * @return
     * @throws Exception
     */
    public static String decrypt(String data, String secret, String salt ) throws Exception {

        SecretKeySpec skeySpec = new SecretKeySpec(secret.getBytes("ASCII"), "AES");

        IvParameterSpec iv = new IvParameterSpec(salt.getBytes());

        byte[] base64Decrypted = Base64.decodeBase64(data.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] plainTextBytes = cipher.doFinal(base64Decrypted);

        String plainText = new String(plainTextBytes, "utf-8");
        return plainText;
    }


    /**
     * 解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String decrypt(String data) throws Exception {
        if (StringUtils.isEmpty(data)) {
            return "";
        }
        String secret = EncryptedString.key;
        String salt = EncryptedString.iv;
        SecretKeySpec skeySpec = new SecretKeySpec(secret.getBytes("ASCII"), "AES");

        IvParameterSpec iv = new IvParameterSpec(salt.getBytes());

        byte[] base64Decrypted = Base64.decodeBase64(data.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] plainTextBytes = cipher.doFinal(base64Decrypted);

        String plainText = new String(plainTextBytes, "utf-8");
        return plainText;
    }

    /**
     * 解密
     *
     * @param secret
     * @param salt
     * @param data
     * @return
     * @throws Exception
     */
    public static String decryptPwd(String data, String secret, String salt ) throws Exception {

        byte[] base64Decrypted = Base64.decodeBase64(data.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        SecretKeySpec skeySpec = new SecretKeySpec(secret.getBytes(), "AES");

        IvParameterSpec iv = new IvParameterSpec(salt.getBytes());


        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        byte[] plainTextBytes = cipher.doFinal(base64Decrypted);

        String plainText = new String(plainTextBytes,"utf-8");
        return plainText;
    }

}
