package com.simuwang.base.common.util;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

/**
 * TODO
 *
 * @author chenjianhua
 * @version 1.0
 * @date 2022/7/31 23:35
 */
public class EncodeUtil {
    private EncodeUtil() {
        throw new Error("该类不能被实例化！");
    }

    private static Cipher makeCipher() throws Exception {
        return Cipher.getInstance("DES");
    }

    private static SecretKey makeKeyFactory(String sKey) {
        SecretKey secretKey = null;

        try {
            SecretKeyFactory des = SecretKeyFactory.getInstance("DES");
            secretKey = des.generateSecret(new DESKeySpec(sKey.getBytes()));
            return secretKey;
        } catch (Exception var3) {
            var3.printStackTrace();
            return secretKey;
        }
    }

    public static String encrypt(String key, String text) {
        try {
            Cipher cipher = makeCipher();
            SecretKey secretKey = makeKeyFactory(key);
            cipher.init(1, secretKey);
            return new String(Base64.getEncoder().encode(cipher.doFinal(text.getBytes())));
        } catch (Exception var4) {
            var4.printStackTrace();
            return text;
        }
    }

    public static String decrypt(String key, String text) {
        try {
            Cipher cipher = makeCipher();
            SecretKey secretKey = makeKeyFactory(key);
            cipher.init(2, secretKey);
            return new String(cipher.doFinal(Base64.getDecoder().decode(text.getBytes())));
        } catch (Exception var4) {
            var4.printStackTrace();
            return text;
        }
    }

    public static final String md5(String s) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

        try {
            byte[] strTemp = s.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;

            for(int i = 0; i < j; ++i) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 15];
                str[k++] = hexDigits[byte0 & 15];
            }

            return new String(str);
        } catch (Exception var10) {
            return null;
        }
    }

    public static String encodeUTF8(String source) {
        String result = source;

        try {
            result = URLEncoder.encode(source, StandardCharsets.UTF_8.name());
            result = result.replace("+", "%20");
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
        }

        return result;
    }

    public static String decodeUTF8(String source) {
        String result = source;

        try {
            result = URLDecoder.decode(source, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
        }

        return result;
    }

    public static String AESEncrypt(String content, String key) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(key.getBytes());
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(1, secretKeySpec);
            byte[] byteRresult = cipher.doFinal(byteContent);
            StringBuffer sb = new StringBuffer();

            for(int i = 0; i < byteRresult.length; ++i) {
                String hex = Integer.toHexString(byteRresult[i] & 255);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }

                sb.append(hex.toUpperCase());
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException var13) {
            var13.printStackTrace();
        } catch (NoSuchPaddingException var14) {
            var14.printStackTrace();
        } catch (InvalidKeyException var15) {
            var15.printStackTrace();
        } catch (UnsupportedEncodingException var16) {
            var16.printStackTrace();
        } catch (IllegalBlockSizeException var17) {
            var17.printStackTrace();
        } catch (BadPaddingException var18) {
            var18.printStackTrace();
        }

        return null;
    }

    public static String AESDecrypt(String content, String key) {
        if (content.length() < 1) {
            return null;
        } else {
            byte[] byteRresult = new byte[content.length() / 2];

            for(int i = 0; i < content.length() / 2; ++i) {
                int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16);
                byteRresult[i] = (byte)(high * 16 + low);
            }

            try {
                KeyGenerator kgen = KeyGenerator.getInstance("AES");
                SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
                random.setSeed(key.getBytes());
                kgen.init(128, random);
                SecretKey secretKey = kgen.generateKey();
                byte[] enCodeFormat = secretKey.getEncoded();
                SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(2, secretKeySpec);
                byte[] result = cipher.doFinal(byteRresult);
                return new String(result);
            } catch (NoSuchAlgorithmException var10) {
                var10.printStackTrace();
            } catch (NoSuchPaddingException var11) {
                var11.printStackTrace();
            } catch (InvalidKeyException var12) {
                var12.printStackTrace();
            } catch (IllegalBlockSizeException var13) {
                var13.printStackTrace();
            } catch (BadPaddingException var14) {
                var14.printStackTrace();
            }

            return null;
        }
    }
}
