package com.tcl.ah.security;

import org.apache.commons.codecc.binary.Base64;

import java.io.IOException;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;


public class KeyHelper {
    //暂时不支持中文
//    public static String encrypt(String data,String key){
//        //1.获取key的md5 码字符串 A
//        //2.将获取A 字符串进行反转得到B
//        //3.data数据调整顺序:将第0到第四位插入到倒数第三位之前，得到C
//        //4.将key的第二位后的三位+得到的C+key的第5位后的一位，得到D
//        //5.根据C的长度，创建一个长度一样的串(长度不足，以D重复填充)，得到E
//        //6.将C和E按位相加，然后模256取余得到字符串，得到G
//        //7.将G 通过base64 编码，然后返回
//        try {
//            String keyMd5 = MD5.getHash(key);
//            StringBuilder reverseKeyMd5 =new StringBuilder(keyMd5).reverse();
//            StringBuilder newData = new StringBuilder();
//            newData.append(data,5,data.length()-3)
//                    .append(data,0,5)
//                    .append(data,data.length()-3,data.length());
//            newData.insert(0,reverseKeyMd5,2,2+3)
//                    .append(reverseKeyMd5,5,5+1);
//            StringBuilder newKey = new StringBuilder();
//            int len = newData.length();
//            for(int i = 0;i<len;i++){
//                newKey.append(reverseKeyMd5.charAt(i%reverseKeyMd5.length()));
//            }
//            StringBuilder builder = new StringBuilder();
//            ByteBuffer byteBuffer= ByteBuffer.allocate(len);
//            for(int i=0;i<len;i++){
//                byteBuffer.put((byte)(newData.charAt(i)%256+(newKey.charAt(i)%256)%256));
//                //builder.append((byte)((newData.charAt(i)+newKey.charAt(i))%256));
//            }
//            byte[] target =byteBuffer.array();
//            String targetStr= Base64.encodeBase64String(target);
//
//
//            return targetStr ;
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null ;
//    }

    //暂时不支持中文
//    public static String decrypt(String data,String key){
//        String keyMd5 = null;
//        try {
//            keyMd5 = MD5.getHash(key);
//            StringBuilder reverseKeyMd5 = new StringBuilder(keyMd5).reverse();
//            byte[] newData= Base64.decodeBase64(data);
//            int len = newData.length;
//            StringBuilder newKey = new StringBuilder();
//            for(int i = 0;i<len;i++){
//                newKey.append(reverseKeyMd5.charAt(i%reverseKeyMd5.length()));
//            }
//            StringBuilder builder = new StringBuilder();
//            for(int i = 0;i<len;i++){
//                byte dataByte =(byte)(newData[i]%256);
//                byte keyByte =(byte)((newKey.charAt(i)%256));
//                if(dataByte<keyByte) {
//                    builder.append((char)(dataByte + 256 - keyByte));
//                }else{
//                    builder.append((char)(dataByte-keyByte));
//                }
//            }
//            String value="eb3OOk8/729QJSOj3mNiQzEXwRtEpQDpKz/YrnO6dnXRes=f";
//            builder.delete(0,3)
//                    .delete(builder.length()-1,builder.length());
//            StringBuilder decrypt = new StringBuilder();
//            decrypt.append(builder,builder.length()-8,builder.length()-3)
//                    .append(builder,0,builder.length()-8)
//                    .append(builder,builder.length()-3,builder.length());
//            return decrypt.toString();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }
//        return null ;
//    }


    public static String encrypt(String data, String key) throws IOException, NoSuchAlgorithmException {
        //1.获取key的md5 码字符串 A
        //2.将获取A 字符串进行反转得到B
        //3.data数据调整顺序:将第0到第四位插入到倒数第三位之前，得到C
        //4.将key的第二位后的三位+得到的C+key的第5位后的一位，得到D
        //5.根据C的长度，创建一个长度一样的串(长度不足，以D重复填充)，得到E
        //6.将C和E按位相加，然后模256取余得到字符串，得到G
        //7.将G 通过base64 编码，然后返回
        String keyMd5 = MD5.getHash(key);
        byte[] breverseKeyMd5 =reverseByByte(keyMd5);
        byte[] bdata=data.getBytes("UTF-8");
        byte[] newData = new byte[bdata.length+4];
        System.arraycopy(breverseKeyMd5,2,newData,0,3);

        System.arraycopy(bdata,5,newData,3,bdata.length-3-5);
        System.arraycopy(bdata,0,newData,3+bdata.length-3-5,5);
        System.arraycopy(bdata,bdata.length-3,newData,3+bdata.length-3,3);

        System.arraycopy(breverseKeyMd5,5,newData,bdata.length+3,1);

        int len = newData.length;
        byte[] newKey = new byte[len];
        for(int i = 0;i<len;i++){
            newKey[i]=breverseKeyMd5[i%breverseKeyMd5.length];
        }

        byte[] encryptData= new byte[len];
        for(int i=0;i<len;i++){
            byte dataByte =(byte)(newData[i]%256);
            byte keyByte =(byte)((newKey[i]%256));
            encryptData[i] =(byte)((dataByte+keyByte)%256);
        }
        String targetStr= Base64.encodeBase64String(encryptData);
        return targetStr ;
    }


    public static String decrypt(String data, String key) throws IOException, NoSuchAlgorithmException {
        String keyMd5 = MD5.getHash(key);
        byte[] breverseKeyMd5 = reverseByByte(keyMd5);
        byte[] bData= Base64.decodeBase64(data);
        int len = bData.length;

        if(len<12){
            throw new IOException("data is illegal");
        }
        byte[] newKey = new byte[len];
        for(int i = 0;i<len;i++){
            newKey[i]=breverseKeyMd5[i%breverseKeyMd5.length];
        }

        byte[] encryptData= new byte[len];
        for(int i = 0;i<len;i++){
            byte dataByte =(byte)(bData[i]%256);
            byte keyByte =(byte)((newKey[i]%256));
            if(dataByte<keyByte) {
                encryptData[i]=(byte)((dataByte + 256 - keyByte));
            }else{
                encryptData[i]=(byte)(dataByte-keyByte);
            }
        }
        byte[] newData = new byte[encryptData.length-3-1];
        System.arraycopy(encryptData,encryptData.length-8-1,newData,0,5);
        System.arraycopy(encryptData,3,newData,5,encryptData.length-8-3-1);
        System.arraycopy(encryptData,encryptData.length-3-1,newData,encryptData.length-3-1-3,3);
        return new String(newData);
    }

    private static byte[] reverseByByte(String src){
        byte[] targetByte =src.getBytes(Charset.forName("UTF-8"));
        int len = targetByte.length-1;
        for(int i=(len-1)>>1;i>=0;i--){
            int k = len-i;
            byte bi = targetByte[i];
            byte bk = targetByte[k];
            targetByte[k]=bi;
            targetByte[i]=bk;
        }
        return targetByte;
    }
}
