package com.tzy.common.utils;

import com.tzy.common.constant.Constants;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class AESUtil {
    /**  密钥算法 */
    private static final String ALGORITHM = "AES";
    /**  加解密算法/工作模式/填充方式  */
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS5Padding";

    public static void main(String[] args) throws Exception {
        String password = Constants.AES_TOKEN_PASSWORD;
        //String s = encryptData("我们常常在某些软件",password );
        //System.out.println(s);
        //String s1 = decryptData(s, password);
        //System.out.println(s1);
        //System.out.println(MD5("12340000"));
        //System.out.println(MD5( Constants.AES_TOKEN_PASSWORD));
        //System.out.println(new Md5Hash("admin_dev" + "123456" + "167ebc").toHex());
        //Map<String, String> signData = new TreeMap<>();
        //signData.put("ids","1");
        //signData.put("times",System.currentTimeMillis()+"");
        //signData.put("userId","111");
        //signData.put("uuid", UUID.randomUUID().toString().replaceAll("_",""));
        //String signStr = signData.toString().replace("{", "").replaceAll("}", "").replaceAll(",","&").replaceAll(" ","");
        //System.out.println(signStr);
        //System.out.println(decryptData("IgfbVNeDBhy8VIhneneoBqbXg1qq/7BB4GmP/7434mW3kBtSOJdsXKbBZjSRaQMZgVaYamHiKfOg3pJuOujjXQuBKnpFkTVKA1rqxpDqR5w=",Constants.AES_USER_PASSWORD));
        //String s = decryptData("hOnXoKMDAPN8teUmsEKr3LL5rIGQCHgKwVfLOcvxJ6D+gT2I8V53HtmrV+yjDnRCIbLnBB4O0nbo73I5lvrIhwuBKnpFkTVKA1rqxpDqR5w=", Constants.AES_USER_PASSWORD);
        //JSONObject jsonObject = JSON.parseObject(s);
        //jsonObject.put("certName","周泊君");
        //System.out.println( encryptData(jsonObject.toString(),Constants.AES_USER_PASSWORD));
    }

    /** AES加密  */
    public static String encryptData(String data,String password) throws Exception {
        // 创建密码器
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        SecretKeySpec key = new SecretKeySpec(MD5(password).toLowerCase().getBytes(), ALGORITHM);
        // 初始化
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64Util.encode(cipher.doFinal(data.getBytes()));
    }

    /** AES解密 */
    public static String decryptData(String base64Data,String password) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        SecretKeySpec key = new SecretKeySpec(MD5(password).toLowerCase().getBytes(), ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decode = Base64Util.decode(base64Data);
        byte[] doFinal = cipher.doFinal(decode);
        return new String(doFinal,"utf-8");
    }


    public static String MD5(String input) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            return "check jdk";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        char[] charArray = input.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().substring(0,16);
    }
}
