//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.fufan.mtsaas.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EnUtil {
    private static final byte[] IV = new byte[8];
    private static final String _MAIN_KEY = "E84D768E9952E066";

    public EnUtil() {
    }

    public static String xorAndGenMac(String xml, String workkey) throws UnsupportedEncodingException {
        String mac = getMac(xml.getBytes("UTF-8"), HexString2Bytes(workkey));
        return mac;
    }

    public static byte[] HexString2Bytes(String src) {
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();

        for(int i = 0; i < src.length() / 2; ++i) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }

        return ret;
    }

    private static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0}));
        _b0 = (byte)(_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1}));
        byte ret = (byte)(_b0 ^ _b1);
        return ret;
    }

    public static void printHexString(String hint, byte[] b) {
        for(int i = 0; i < b.length; ++i) {
            String hex = Integer.toHexString(b[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            System.out.print(hex.toUpperCase() + " ");
        }

        System.out.println();
    }

    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);

        for(int i = 0; i < bArray.length; ++i) {
            String sTemp = Integer.toHexString(255 & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }

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

        return sb.toString();
    }

    public static byte[] dataXOR(byte[] Input) {
        int length = Input.length;
        int x = length % 8;
        int addLen = 0;
        if (x != 0) {
            addLen = 8 - length % 8;
        }

        int pos = 0;
        byte[] data = new byte[length + addLen];
        System.arraycopy(Input, 0, data, 0, length);
        byte[] oper1 = new byte[8];
        System.arraycopy(data, pos, oper1, 0, 8);
        pos = pos + 8;

        for(int i = 1; i < data.length / 8; ++i) {
            byte[] oper2 = new byte[8];
            System.arraycopy(data, pos, oper2, 0, 8);
            byte[] t = bytesXOR(oper1, oper2);
            oper1 = t;
            pos += 8;
        }

        return oper1;
    }

    public static String getMac(byte[] data, byte[] tmpWorkkey) {
        byte[] workKey = decryptKey(tmpWorkkey, HexString2Bytes("E84D768E9952E066"));
        byte[] resultBlock = dataXOR(data);
        return bytesToHexString(encrypt(resultBlock, workKey));
    }

    private static byte[] decryptKey(byte[] src, byte[] key) {
        try {
            DESKeySpec dks = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
            cipher.init(2, securekey);
            return cipher.doFinal(src);
        } catch (NoSuchAlgorithmException var6) {
            ;
        } catch (InvalidKeyException var7) {
            ;
        } catch (InvalidKeySpecException var8) {
            ;
        } catch (NoSuchPaddingException var9) {
            ;
        } catch (IllegalBlockSizeException var10) {
            ;
        } catch (BadPaddingException var11) {
            ;
        }

        return null;
    }

    private static byte[] encrypt(byte[] src, byte[] key) {
        new SecureRandom();

        try {
            DESKeySpec dks = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            Key securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding");
            IvParameterSpec ivp = new IvParameterSpec(IV);
            cipher.init(1, securekey, ivp);
            return cipher.doFinal(src);
        } catch (NoSuchAlgorithmException var8) {
            ;
        } catch (InvalidKeyException var9) {
            ;
        } catch (InvalidKeySpecException var10) {
            ;
        } catch (NoSuchPaddingException var11) {
            ;
        } catch (IllegalBlockSizeException var12) {
            ;
        } catch (BadPaddingException var13) {
            ;
        } catch (InvalidAlgorithmParameterException var14) {
            var14.printStackTrace();
        }

        return null;
    }

    private static SecretKey getSecretKeySpec(byte[] keyB) throws NoSuchAlgorithmException, InvalidKeySpecException {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("Des");
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyB, "Des");
        return secretKeyFactory.generateSecret(secretKeySpec);
    }

    private static byte byteXOR(byte src, byte src1) {
        return (byte)(src & 255 ^ src1 & 255);
    }

    private static byte[] bytesXOR(byte[] src, byte[] src1) {
        int length = src.length;
        if (length != src1.length) {
            return null;
        } else {
            byte[] result = new byte[length];

            for(int i = 0; i < length; ++i) {
                result[i] = byteXOR(src[i], src1[i]);
            }

            return result;
        }
    }

    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><MESSAGE><MHEAD><VERSION>1.0</VERSION><MERNO>822391059440001</MERNO><TERID>91014358</TERID><PDATE>20170110</PDATE><PTIME>174324</PTIME><RESERVED1/></MHEAD><MBODY><PROCCODE>P1001</PROCCODE><POLICYNO>01100013</POLICYNO></MBODY></MESSAGE>";
        String workkey = "9999999999999999";

        try {
            System.out.println(xml.length());
            System.out.println(Integer.toHexString(xml.length()));
            System.out.println(xorAndGenMac(xml, workkey));
        } catch (UnsupportedEncodingException var4) {
            var4.printStackTrace();
        }

    }
}
