package com.jkcredit.invoice.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.Key;

/**
 * Created by Administrator on 2017/11/25.
 * base64加解密工具类
 */
public class LogUtils {
	private static final String PASSWORD_CRYPT_KEY = "3cm.xin.";
    /**
     * @param bytes
     * @return
     * @throws Exception 
     */
    public static String decode(String data) throws Exception {
    	JSONObject object = JSONObject.parseObject(data);
    	if(object.get("mobile") != null) {
    		object.put("mobile", decryptDES(object.getString("mobile")));
    	}
    	if(object.get("name") != null) {
    		object.put("name", decryptDES(object.getString("name")));
    	}
    	if(object.get("id_number") != null) {
    		object.put("id_number", decryptDES(object.getString("id_number")));
    	}
    	if(object.get("idcard") != null) {
    		object.put("idcard", decryptDES(object.getString("idcard")));
    	}
    	if(object.get("bank_card_number") != null) {
    		object.put("bank_card_number", decryptDES(object.getString("bank_card_number")));
    	}
    	if(object.get("bank_no") != null) {
    		object.put("bank_no", decryptDES(object.getString("bank_no")));
    	}
        return object.toString();
    }

    /**
     * 二进制数据编码为BASE64字符串
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(String data) {
    	try {
			data = URLDecoder.decode(data, "utf-8");
			JSONObject object = JSONObject.parseObject(data);
	    	if(object.get("mobile") != null) {
	    		object.put("mobile", encryptDES(object.getString("mobile")));
	    	}
	    	if(object.get("name") != null) {
	    		object.put("name",encryptDES(object.getString("name")));
	    	}
	    	if(object.get("id_number") != null) {
	    		object.put("id_number", encryptDES(object.getString("id_number")));
	    	}
	    	if(object.get("idcard") != null) {
	    		object.put("idcard", encryptDES(object.getString("idcard")));
	    	}
	    	if(object.get("bank_card_number") != null) {
	    		object.put("bank_card_number", encryptDES(object.getString("bank_card_number")));
	    	}
	    	if(object.get("bank_no") != null) {
	    		object.put("bank_no", encryptDES(object.getString("bank_no")));
	    	}
	        return object.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	return data;
    }
    
    /**
	 * 加密数据
	 * @param encryptString  注意：这里的数据长度只能为8的倍数
	 * @param encryptKey
	 * @return
	 * @throws Exception
	 */
	public static String encryptDES(String encryptString) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(encryptString);
			for (int i = 0; i < 8 - encryptString.getBytes("UTF-8").length % 8 ; i++) {
				sb.append("=");
			}
			encryptString = sb.toString();
			SecretKeySpec key = new SecretKeySpec(getKey(PASSWORD_CRYPT_KEY), "DES");
			Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF-8"));
			return Base64.encodeBase64String(encryptedData);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return encryptString;
	}
	
	/***
	 * 解密数据
	 * @param decryptString
	 * @param decryptKey
	 * @return
	 * @throws Exception
	 */
	public static String decryptDES(String decryptString) throws Exception {
		SecretKeySpec key = new SecretKeySpec(getKey(PASSWORD_CRYPT_KEY), "DES");
		Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte decryptedData[] = cipher.doFinal(Base64.decodeBase64(decryptString));
		return new String(decryptedData).replaceAll("=", "");
	}
	
	/**
	 * 自定义一个key
	 * @param string 
	 */
	public static byte[] getKey(String keyRule) {
		Key key = null;
		byte[] keyByte = keyRule.getBytes();
		// 创建一个空的八位数组,默认情况下为0
		byte[] byteTemp = new byte[8];
		// 将用户指定的规则转换成八位数组
		for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
			byteTemp[i] = keyByte[i];
		}
		key = new SecretKeySpec(byteTemp, "DES");
		return key.getEncoded();
	}
    public static void main(String[] args) throws Exception {
		String a = "{\"id_number\":\"210703199104012012\",\"bank_card_number\":\"321323199812092885\",\"name\":\"刘玺\",\"mobile\":\"15991856264\",\"sign\":\"a93471e386fed0427c68bd6e9967011b\"}";
		System.out.println(encode(a));
		String b = "{\"sign\":\"a93471e386fed0427c68bd6e9967011b\",\"id_number\":\"NjEyMzIzMTk5MjA5MDg0ODcy\",\"name\":\"5YiY5466\",\"bank_card_number\":\"NjIxNDY4MDA0ODU0NTA1NQ==\",\"mobile\":\"MTU4NzMwNTI4MzY=\"}";
		System.out.println(decode(encode(a))); 
    }
}
