package com.fufan.mtsaas.tools;

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

/**
 * 加密工具类
 *
 * @version 1.0
 * @auther Liby
 * @create 2018/3/30
 **/
public class AESTool {

    public static final String DEFAULT_CODING = "utf-8";

//    private static final String KEY = "0DD65C01B83E83CE";
    //256位32个字节 的 key 用于通讯
    private static final String KEY = "UBDUX82VPHKDKB284D7NKJFONCKWBUKA";

    //用于密码
    private static final String ANKEY = "UBDUX82VPHKDKB280DD65C01B83E83CE";


    /**
     * 解密
     *
     * @param encrypted
     * @return
     * @throws Exception
     * @author lmiky
     * @date 2014-2-25
     */
    public static String decrypt(String encrypted) throws Exception {
        byte[] keyb = KEY.getBytes(DEFAULT_CODING);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(keyb);
        SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");
        Cipher dcipher = Cipher.getInstance("AES");
        dcipher.init(Cipher.DECRYPT_MODE, skey);
        byte[] clearbyte = dcipher.doFinal(toByte(encrypted));
        return new String(clearbyte);
    }

    /**
     * 加密
     *
     * @param content
     * @return
     * @throws Exception
     * @author lmiky
     * @date 2014-2-25
     */
    public static String encrypt(String content) throws Exception {
        byte[] input = content.getBytes(DEFAULT_CODING);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(KEY.getBytes(DEFAULT_CODING));
        SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skc);
        byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
        int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
        ctLength += cipher.doFinal(cipherText, ctLength);
        return parseByte2HexStr(cipherText);
    }


    /**
     * 解密 用自定义Key
     *
//     * @param encrypted
//     * @param key
//     * @return
//     * @throws Exception
//     * @author Liby
     * @date 2023-04-20
     */
    public static String decryptKey(String encrypted, String key) throws Exception {
        byte[] keyb = key.getBytes(DEFAULT_CODING);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(keyb);
        SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");
        Cipher dcipher = Cipher.getInstance("AES");
        dcipher.init(Cipher.DECRYPT_MODE, skey);
        byte[] clearbyte = dcipher.doFinal(toByte(encrypted));
        return new String(clearbyte);
    }

    /**
     * 加密 用自定义Key
     *
     * @param content
     * @param key
     * @return
     * @throws Exception
     * @author Liby
     * @date 2023-04-20
     */
    public static String encryptKey(String content, String key) throws Exception {
        byte[] input = content.getBytes(DEFAULT_CODING);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(key.getBytes(DEFAULT_CODING));
        SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skc);
        byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
        int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
        ctLength += cipher.doFinal(cipherText, ctLength);
        return parseByte2HexStr(cipherText);
    }

    /**
     * 字符串转字节数组
     *
     * @param hexString
     * @return
     * @author lmiky
     * @date 2014-2-25
     */
    private static byte[] toByte(String hexString) {
        int len = hexString.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++) {
            result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
        }
        return result;
    }

    /**
     * 字节转16进制数组
     *
     * @param buf
     * @return
     * @author lmiky
     * @date 2014-2-25
     */
    private static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
//        System.out.println(AESTool.encrypt("18893136625"));
//        System.out.println(AESTool.encrypt("18309319905"));
//        System.out.println(AESTool.encrypt("13519661888"));
//        System.out.println(AESTool.encrypt("13893330712"));
//        System.out.println(AESTool.encrypt("13919032822"));
//        System.out.println(AESTool.encrypt("18109446052"));
//        System.out.println(AESTool.encrypt("18709903991"));
//        System.out.println(AESTool.encrypt("13919423235"));
//        System.out.println(AESTool.encrypt("13919185948"));
//        System.out.println(AESTool.encrypt("18153630898"));
//        System.out.println(AESTool.decrypt("9ba6a83c57c6fb04e2a9687a98c9f874"));
//        System.out.println(AESTool.encryptKey("18738478757", ANKEY));
//        System.out.println(AESTool.decryptKey("bc4855e2cefce2732083d2f3e8bb1738", ANKEY));
//        System.out.println("18121298510 - " + AESTool.encrypt("18121298510"));
//        System.out.println(AESTool.decrypt("2598d5fddee354a7e75494c766696adf"));
        System.out.println(AESTool.encrypt("#7sT9kL$2pQx&"));


    }
}


