package com.zcxk.core.utils.encrypt;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
 * @author Andy
 * @description: 使用DES加密和解密工具类
 * @date 2021/7/16
 * @version V1.0
 **/
public class DesUtil {

    /**
     * 密钥的key值
     */
    private Key key;

    private byte[] desKey;

    private byte[] desiv = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD,
        (byte) 0xEF};

    /**
     * 加密算法的参数接口
     */
    private AlgorithmParameterSpec iv = null;

    /**
     * 加密密钥
     */
    public DesUtil(String deSkey) {
        try {
            if (deSkey == null || deSkey.trim().length() == 0) {
                throw new IllegalArgumentException("DESkey 参数是加密密钥，不能为空");
            } else {
                // 设置密钥
                this.desKey = deSkey.trim().getBytes("UTF-8");
            }
            // 设置密钥参数
            DESKeySpec keySpec = new DESKeySpec(this.desKey);
            // 设置向量
            iv = new IvParameterSpec(desiv);
            // 获得密钥工厂
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // 得到密钥对象
            key = keyFactory.generateSecret(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加密String 明文输入密文输出
     * @param inputString 待加密的明文
     * @return 加密后的字符串
     */
    public String getEnc(String inputString) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String outputString = "";
        try {
            byteMing = inputString.getBytes("UTF-8");
            byteMi = this.getEncCode(byteMing);
            outputString = Base64.encode(byteMi);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outputString;
    }

    /**
     * 解密String 以密文输入明文输出
     *
     * @param inputString 需要解密的字符串
     * @return 解密后的字符串
     */
    public String getDec(String inputString) {
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = "";
        try {
            byteMi = Base64.decode(inputString);
            byteMing = this.getDesCode(byteMi);
            strMing = new String(byteMing, "UTF8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMing;
    }

    /**
     * 加密以byte[]明文输入,byte[]密文输出
     *
     * @param bt 待加密的字节码
     * @return 加密后的字节码
     */
    private byte[] getEncCode(byte[] bt) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            // 得到Cipher实例
            cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
            byteFina = cipher.doFinal(bt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteFina;
    }

    /**
     * 解密以byte[]密文输入,以byte[]明文输出
     *
     * @param bt 待解密的字节码
     * @return 解密后的字节码
     */
    private byte[] getDesCode(byte[] bt) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            // 得到Cipher实例
            cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            byteFina = cipher.doFinal(bt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteFina;
    }
}
