package com.lightinit.hsdatashow.common;


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

/**
 * Created by Mr.Yao on 2016/1/18.
 */
public class TripleDESUtilsPro {
    private static String encypt_des_key="lantinit.com";
    private static final String Algorithm = "DESede";  //定义 加密算法,可用 DES,DESede,Blowfish
    private static final char[] HEX_CHAR= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    // 加密字符串
    public static byte[] encryptMode(byte[] src) {
        try { // 生成密钥
            SecretKey deskey = new SecretKeySpec(build3DesKey(encypt_des_key), Algorithm); // 加密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }
    public static String encryptMode(String src) {
        try {
            byte[]decryptByte=encryptMode(src.getBytes("utf-8"));
            if (decryptByte==null)
                return "";
            return byteArrayToString(decryptByte);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
    // 解密字符串
    public static byte[] decryptMode(byte[] src) {
        try { // 生成密钥
            SecretKey deskey = new SecretKeySpec(build3DesKey(encypt_des_key), Algorithm); // 解密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }
    public static String decryptMode(String src) {
        try {
            byte[]decryptByte=decryptMode(HexString2Bytes(src));
            if (decryptByte==null)
                return "";
            return new String(decryptByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String decryptHex(String src){
        byte[]srcByte=HexString2Bytes(src);
        byte[]decryptByte=decryptMode(srcByte);
        if (decryptByte==null)
            return "";
        try {
            return new String(decryptByte,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
    public static byte[] HexString2Bytes(String hexstr) {
        byte[] b = new byte[hexstr.length() / 2];
        int j = 0;
        for (int i = 0; i < b.length; i++) {
            char c0 = hexstr.charAt(j++);
            char c1 = hexstr.charAt(j++);
            b[i] = (byte) ((parse(c0) << 4) | parse(c1));
        }
        return b;
    }
    public static String byteArrayToString(byte[] data){
        StringBuilder stringBuilder= new StringBuilder();
        for (int i=0; i<data.length; i++){
            stringBuilder.append(HEX_CHAR[(data[i] & 0xf0)>>> 4]);
            stringBuilder.append(HEX_CHAR[(data[i] & 0x0f)]);
        }
        return stringBuilder.toString();
    }
    private static int parse(char c) {
        if (c >= 'a')
            return (c - 'a' + 10) & 0x0f;
        if (c >= 'A')
            return (c - 'A' + 10) & 0x0f;
        return (c - '0') & 0x0f;
    }

    public static byte[] build3DesKey(String keyStr){
        byte[] key = new byte[24];    //声明一个24位的字节数组，默认里面都是0
        try {
            byte[] temp = keyStr.getBytes("UTF-8");    //将字符串转成字节数组
            if (key.length > temp.length) {
                System.arraycopy(temp, 0, key, 0, temp.length);
            } else {
                System.arraycopy(temp, 0, key, 0, key.length);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return key;
    }
}
