package psdi.util;

//import psdi.webclient.applet.attachimage.Base64;

/**
 * 作者：雨亦奇
 * 从 Maxim6x 秦皇岛发电厂成功案例中自动转出，并在maximo7.5 的基础上进行修改方法测试，完全正确！
 */
public class MXCustomCipher {

    public MXCustomCipher() {
    }
    public  static void main(String args[])
    {
        try{
            MXCustomCipher custcipher = new MXCustomCipher();
            //System.out.println("password begin....");
            //测试加密后解密 Begin
            //String password = custcipher.decData("52266F3FDD0AEFD7AE3BF4B172794163");
            //String password = custcipher.decData("10FE6F4650B2ACB49A2121D7E6133E64");
            //String password = custcipher.decData("21CF877D7738DBADA0B2FADEFEBD9558");
            //String password = custcipher.decData("500C7C9864165AD6E8A35E1EFC5279C2");
            //String password = custcipher.decData("21CF877D7738DBADA0B2FADEFEBD9558");//赵俊玲 密码，无法解密
            //String password = custcipher.decData("500C7C9864165AD6E8A35E1EFC5279C6");//赵俊玲 密码，可以解密
            //String password = custcipher.decData("B0F52A8471221C34CE22DBE64C5B1DB7B1524678B8B780C1");//xpiY@1zN，可以解密
            //String password = custcipher.decData("9B4481608612C8FF7D551DE75E597DDFAA0583690D069479");//xpiY@1zN，可以解密
//            String password = custcipher.decData("10FE6F4650B2ACB42FF8562952D618F1CB148683998A03BC");//xpiY@1zN，可以解密
            String password = custcipher.decData("10FE6F4650B2ACB49A2121D7E6133E64");//xpiY@1zN，可以解密
            //                                      BB89A704C4B02304A58221981B1F61DF8DD8810F4A6D6DB3
            //String password = custcipher.decData("21CF877D7738DBADA0B2FADEFEBD9558");
            //String password = custcipher.decData("F59BD65F7EDAFB087A81D4DCA06C4910");//xpiY@1zN，可以解密
            //O*O1TeLK
            //String password = custcipher.decData("C33367701511B4F6");
            System.out.println("password="+password);
            //String password = custcipher.encData("111111");
            //System.out.println("password="+password);
            //测试加密后解密 End

            //测试加密  Begin
            //String enpassword = custcipher.encData("maxadmin");
            //System.out.println("enpassword="+enpassword);
            //测试加密  Begin
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    /**
     * 对明码口令字符串进行加密，生成加密后的字符串
     * @param
     * @return String
     */
    public static String encData( String data)
            throws MXException,java.rmi.RemoteException
    {
        MXCipher mxcipher = new MXCipher("DESede", "CBC", "PKCS5Padding", "Sa#qk5usfmMI-@2dbZP9`jL3", "beLd7$lB", null, null);
        byte abyte0[] = mxcipher.encData(data);
        String encdata = bytes2HexString(abyte0);
        return encdata;
    }

    /**
     * 对加密后口令字符串进行解密，生成明码口令字符串
     * @param
     * @return String
     */
    public static String decData( String encdata)
            throws MXException,java.rmi.RemoteException
    {
        String data = null;
        //System.out.println("password begin 1....");
        MXCipher mxcipher = new MXCipher("DESede", "CBC", "PKCS5Padding", "Sa#qk5usfmMI-@2dbZP9`jL3", "beLd7$lB", null, null);
        try{
            //byte abyte0[] = HexString2Bytes(encdata);//原来写法！
            byte abyte0[] = hex2byte(encdata.getBytes("UTF-8"));
            //byte abyte0[] = Base64.decode(encdata);
            data = mxcipher.decData(abyte0);
        }catch(Exception e){
            System.out.print(e);
        }
        return data;
    }

    /**
     * 2进制转换16进制
     * @param b byte[]
     * @return String
     */
    public static String bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }

    /**
     * 将指定字符串src，以每两个字符分割转换为16进制形式
     * 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9}
     * @param src String
     * @return byte[]
     */
    public static byte[] HexString2Bytes(String src){
        int srclen = src.length();
        byte[] ret = new byte[srclen/2];
        byte[] tmp = src.getBytes();
        for(int i=0; i<srclen/2; i++){
            ret[i] = unitBytes(tmp[i*2], tmp[i*2+1]);
        }
        return ret;
    }
    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("长度不是偶数");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }
    /**
     * 将两个ASCII字符合成一个字节；
     * 如："EF"--> 0xEF
     * @param src0 byte
     * @param src1 byte
     * @return byte
     */
    public static byte unitBytes(byte src0, byte src1) {
        byte b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        b0 = (byte)(b0 << 4);
        byte b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        byte ret = (byte)(b0 ^ b1);
        return ret;
    }
}
