package com.winsun.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

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

/**
 * @author chengfengluo
 * @date 2021-02-19 14:58
 */
public class EThreeDES {

    private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish

    //keybyte为加密密钥，长度为24字节
    //src为被加密的数据缓冲区（源）

    //3DES加密
    public static byte[] encryptMode(byte[] keybyte, byte[] src) {
        try {
            //生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, 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 (Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

    //keybyte为加密密钥，长度为24字节
    //src为加密后的缓冲区

    //3DES解密
    public static byte[] decryptMode(byte[] keybyte, byte[] src) throws Exception {
        try {
            //生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, 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();
        }
        return null;
    }

    //转换成十六进制字符串
    public static String byte2hex(byte[] b) {
        String hs="";
        String stmp="";

        for (int n=0;n<b.length;n++) {
            stmp=(Integer.toHexString(b[n] & 0XFF));
            if (stmp.length()==1) hs=hs+"0"+stmp;
            else hs=hs+stmp;
            if (n<b.length-1)  hs=hs+":";
        }
        return hs.toUpperCase();
    }

    //转为base64
    public static String enBase64(byte[] bparm) throws IOException {
        BASE64Encoder enc=new BASE64Encoder();
        String bdnParm = enc.encodeBuffer(bparm);
        return bdnParm;
    }

    //从BASE64反转回来
    public static byte[] deBase64(String parm) throws IOException {
        BASE64Decoder dec=new BASE64Decoder();
        byte[] dnParm = dec.decodeBuffer(parm);
        return dnParm;
    }



    public static String replace(String source, String oldString,
                                 String newString) {
        StringBuffer output = new StringBuffer();
        int lengthOfSource = source.length();
        int lengthOfOld = oldString.length();
        int posStart = 0;
        int pos; //
        while ( (pos = source.indexOf(oldString, posStart)) >= 0) {
            output.append(source.substring(posStart, pos));
            output.append(newString);
            posStart = pos + lengthOfOld;
        }
        if (posStart < lengthOfSource) {
            output.append(source.substring(posStart));
        }
        return output.toString();
    }




    /**
     * 3DES解密
     * @param toThreeDES
     * @return
     */
    public static String deThreeDES(String toThreeDES){
        String deThreeDes="";
        if (toThreeDES==null || toThreeDES.equals(""))
        {
            deThreeDes="";
        }
        else
        {
            try{
                EThreeDES edes = new EThreeDES();
                String key_VALUE = "A314BA5A3C85E86KK887WSWS";
                byte[] keyBytes = key_VALUE.getBytes();

                byte[] toBASE64ToStr = edes.deBase64(toThreeDES);
                byte[] toWK_DESToStr = EThreeDES.decryptMode(keyBytes, toBASE64ToStr);
                deThreeDes = new String(toWK_DESToStr,"utf-8");
            }
            catch(Exception ex)
            {
                //System.out.println("3DES解密出错!!!"+ex.getMessage());
            }
        }

        return deThreeDes;
    }



    public static void main(String[] args) throws IOException
    {
        EThreeDES eThreeDES =  new EThreeDES();
        String key_VALUE = "C314BONC3C85E86KK996WSWS";  //密匙

        //加密
        String original  = "{termNo:\"yxsst_ht191106\", orderTime:\"2021-01-27 11:30:00\", prodName:\"套餐名称\", reqCode:\"GZ202101261234567\", subsCode: [\"611234\", \"612345\", \"611226\"], custName:\"张*三\", certNo: \"4401**********1234\", accNbr:\"13888888888\", orderState: \"S0K\", orderStateName:\"已完成/已取卡\", payState: \"1\", crmStateName:\"完工\", crmStateReason:\"000\",  recNo:\"0200202101251030423400027500\"}";
        byte[] eBy = EThreeDES.encryptMode(key_VALUE.getBytes(),original.getBytes());
        String eBase64 =  eThreeDES.enBase64(eBy);
        System.out.println("3DES加密后的字符串:" + eBase64);

        //解密
        String dBase64 = eBase64;
        byte[] dBy = EThreeDES.deBase64(dBase64);
//        byte[] srcBytes = EThreeDES.decryptMode(KEY.getBytes(), dBy);
//        System.out.println("3DES解密后的字符串:" + new String(srcBytes,"utf-8"));

    }

}
