package com.hs.common.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * AES 加密组件
 */
public class AESUtil {
    /**
     * 默认加密密钥
     */
    private final static String DEFAULT_KEY = "L♥L";
    /**
     * 默认字符编码格式
     */
    private final static String DEFAULT_CHARSET = "utf-8";
    /**
     * 随机源生成算法
     */
    private final static String RANDOM_ALGORITHM = "SHA1PRNG";
    /**
     * 密钥生成算法
     */
    private final static String KEY_ALGORITHM = "AES";
    /**
     * 加密算法
     */
    private final static String CIPHER_ALGORITHM = "AES";

    /**
     * 转换对称密钥
     *
     * @return
     * @throws NoSuchAlgorithmException
     */
    private static Key initKey(byte[] key) throws NoSuchAlgorithmException {
        // 1.生成一个128位的随机源
        SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
        random.setSeed(key);
        // 2.构造密钥生成器，指定为AES算法,不区分大小写
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
        // 3.根据加密规则初始化密钥生成器
        keyGenerator.init(128, random);
        // 4.产生原始对称密钥
        SecretKey originalKey = keyGenerator.generateKey();
        // 5.根据原始对称密钥的字节数组生成AES密钥
        return new SecretKeySpec(originalKey.getEncoded(), KEY_ALGORITHM);
    }

    /**
     * 加密
     *
     * @param data 待加密数据
     * @param key  密钥
     * @return
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static String encode(String data, byte[] key)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
            UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
        // 根据指定算法AES生成密码器
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        // 初始化密码器
        cipher.init(Cipher.ENCRYPT_MODE, initKey(key));
        byte[] bytes = cipher.doFinal(data.getBytes(DEFAULT_CHARSET));
        // 将加密后的数据转换为字符串
        return new BASE64Encoder().encode(bytes);
    }

    /**
     * 加密
     *
     * @param data 待加密数据
     * @return
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static String encode(String data)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
            UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
        return encode(data, DEFAULT_KEY.getBytes(DEFAULT_CHARSET));
    }

    /**
     * 解密
     *
     * @param data 加密数据
     * @param key  密钥
     * @return
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws IOException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static String decode(String data, byte[] key)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
            IOException, BadPaddingException, IllegalBlockSizeException {
        // 根据指定算法AES生成密码器
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        // 初始化密码器
        cipher.init(Cipher.DECRYPT_MODE, initKey(key));
        byte[] bytes = cipher.doFinal(new BASE64Decoder().decodeBuffer(data));
        return new String(bytes, DEFAULT_CHARSET);
    }

    /**
     * 解密
     *
     * @param data 加密数据
     * @return
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws IOException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static String decode(String data)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
            IOException, BadPaddingException, IllegalBlockSizeException {
        return decode(data, DEFAULT_KEY.getBytes(DEFAULT_CHARSET));
    }
}
