/*
 * Decompiled with CFR 0.152.
 */
package com.hst.taxService.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {
    public static final String key0 = "455e3a9af9d01fe39368bfe4f3769b0bed92c3a92992f7f40c4265faf4799f62e9405bc80115d0cb3aedb52d88241a45b9e25142d8cb4a87bd1192ff567c89b5d8e13eeda02b2b6442d59667217b3aedbe9953b2cc3132093d882a3094934a381690c958281870a185fe21390af9865a007771d116e12a3254ccbfbc9511f76a9b82fd48e4";
    public static final String key1 = "mIPA6SC3";
    public static final String key2 = "F59E1B2970D64B8F926E9932E0A1A1DB";

    public static void main(String[] args) throws Exception {
        System.out.println(AESUtil.getRandomCharacter(6));
        String randan = AESUtil.getRandomCharacter(6);
        String s = "339901999999141</>" + AESUtil.getMD5(key2 + randan) + "</>" + randan;
        String encrypt = AESUtil.aesEncrypt(s, key1);
        System.out.println("\u52a0\u5bc6\u540e\uff1a" + encrypt);
        String decrypt = AESUtil.aesDecrypt(encrypt, key1);
        System.out.println("\u89e3\u5bc6\u540e\uff1a" + decrypt);
    }

    public static String getContent(String content) throws Exception {
        String encrypt = AESUtil.aesEncryptC(content, key1);
        return encrypt;
    }

    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return AESUtil.parseByte2HexStr(AESUtil.aesEncryptToBytes(content, encryptKey));
    }

    public static String aesEncrypt(String content) throws Exception {
        return AESUtil.parseByte2HexStr(AESUtil.aesEncryptToBytes(content, key1));
    }

    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return AESUtil.aesDecryptByBytes(AESUtil.parseHexStr2Byte(encryptStr), decryptKey);
    }

    public static String aesEncryptC(String encryptStr, String decryptKey) throws Exception {
        return AESUtil.parseByte2HexStr(AESUtil.aesEncryptToBytesC(encryptStr, decryptKey));
    }

    public static String aesDecryptC(String encryptStr, String decryptKey) throws Exception {
        return AESUtil.aesDecryptByBytesC(AESUtil.parseHexStr2Byte(encryptStr), decryptKey);
    }

    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(encryptKey.getBytes());
        kgen.init(128, random);
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(1, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
        return cipher.doFinal(content.getBytes("utf-8"));
    }

    public static String parseByte2HexStr(byte[] buf) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        while (i < buf.length) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = String.valueOf('0') + hex;
            }
            sb.append(hex.toUpperCase());
            ++i;
        }
        return sb.toString();
    }

    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(decryptKey.getBytes());
        kgen.init(128, random);
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(2, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        int i = 0;
        while (i < hexStr.length() / 2) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte)(high * 16 + low);
            ++i;
        }
        return result;
    }

    public static byte[] aesEncryptToBytesC(String content, String encryptKey) {
        try {
            Cipher aesECB = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "AES");
            aesECB.init(1, key);
            byte[] result = aesECB.doFinal(content.getBytes("UTF-8"));
            return result;
        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
        catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        catch (BadPaddingException e) {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getRandomCharacter(int len) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        int i = 0;
        while (i < len) {
            int result = 97 + random.nextInt(25);
            sb.append((char)result);
            ++i;
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

    public static String aesDecryptByBytesC(byte[] encryptBytes, String decryptKey) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "AES");
            cipher.init(2, key);
            return new String(cipher.doFinal(encryptBytes));
        }
        catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
        catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getMD5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            return new BigInteger(1, md.digest()).toString(16);
        }
        catch (Exception exception) {
            return "";
        }
    }
}
