package com.zehong.web.controller.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zehong.common.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;




public class DESEncoder {

	/**
	 * 加密向量不要动(8位)
	 */
	private static String IV = "12345678";
	/**
	 * 加密
	 * @param value 加密内容
	 * @param KEY 加密密钥（8位）
	 * @return
	 */
	public static String encrypt(String value,String KEY) {
		byte[] key = null, iv = null;
		try {
			key = KEY.getBytes("utf-8");
			iv = IV.getBytes("utf-8");
		} catch (UnsupportedEncodingException e1) {

		}
		String result = null;
		byte[] tmpOriginalStr = null;
		try {
			if (!isEmpty(value)) {
				tmpOriginalStr = value.getBytes("utf-8");
				SecretKeyFactory keyFactory = SecretKeyFactory
						.getInstance("DES");
				DESKeySpec dks = new DESKeySpec(key);
				SecretKey secretKey = keyFactory.generateSecret(dks);
				IvParameterSpec param = new IvParameterSpec(iv);
				Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
				cipher.init(Cipher.ENCRYPT_MODE, secretKey, param);
				byte[] tmpEncypt = cipher.doFinal(tmpOriginalStr);
				if (tmpEncypt != null) {
					// BASE64Encoder bse=new BASE64Encoder();
					// result=bse.encode(tmpEncypt);
					byte[] b = com.main.util.Base64_2.encode(tmpEncypt, 0);
					result = parseByte2HexStr(b);
				}
			}
		} catch (Exception e) {
		}
		return result;
	}



	/**
	 * 解密
	 * @param value 要解密的字符串
	 * @param KEY 解密密钥
	 * @return
	 */
	public static java.lang.String decrypt(String value,String KEY) {
		byte[] decrypt = parseHexStr2Byte(value);
		byte[] key = null, iv = null;
		try {
			key = KEY.getBytes("utf-8");
			iv = IV.getBytes("utf-8");
		} catch (UnsupportedEncodingException e1) {

		}
		String result = null;
		byte[] tmpEncrypStr = null;
		try {

				// BASE64Decoder bd=new BASE64Decoder();
				// tmpEncrypStr =bd.decodeBuffer(encrypStr);
				tmpEncrypStr = com.main.util.Base64_2.decode(decrypt, 0);
				SecretKeyFactory keyFactory = SecretKeyFactory
						.getInstance("DES");
				DESKeySpec dks = new DESKeySpec(key);
				SecretKey secretKey = keyFactory.generateSecret(dks);
				IvParameterSpec param = new IvParameterSpec(iv);
				Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
				cipher.init(Cipher.DECRYPT_MODE, secretKey, param);
				byte[] tmpDecrypt = cipher.doFinal(tmpEncrypStr);
				if (tmpDecrypt != null) {
					result = new String(tmpDecrypt, "utf-8");
				}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


    /**将二进制转换成16进制
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < buf.length; i++) {
                    String hex = Integer.toHexString(buf[i] & 0xFF);
                    if (hex.length() == 1) {
                            hex = '0' + hex;
                    }
                    sb.append(hex.toUpperCase());
            }
            return sb.toString();
    }


    /**将16进制转换为二进制
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
            if (hexStr.length() < 1)
                    return null;
            byte[] result = new byte[hexStr.length()/2];
            for (int i = 0;i< hexStr.length()/2; i++) {
                    int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
                    int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
                    result[i] = (byte) (high * 16 + low);
            }
            return result;
    }

	/**
	 * 字符串是否为空
	 *
	 * @param
	 * @return 是或否
	 */
	public static boolean isEmpty(String str) {
		if (str == null) {
			return true;
		} else if (str.equals("") || str.equals("null")) {
			return true;
		} else {
			return false;
		}
	}


	public static void main(String args[]){
//		String skcs1 = "{\"username\":\"skcs1\",\"password\":\"czyj@123\",\"timestamp\":\"2020-07-28 16:22:00\"}";
//		System.out.println(DESEncoder.encrypt(skcs1,"23546324"));

//		String lpggasdata = "[\n" +
//				"\t{\n" +
//				"\n" +
//				"\t\t\"userOwnId\":\"0001\",\n" +
//				"\t\t\"userType\":\"pl\",\n" +
//				"\t\t\"controlPlan\":\"方案1\",\n" +
//				"\t\t\"updateTime\":\"2024-12-07 15:20:11\",\n" +
//				"\t\t\"conStartTime\":\"2024-12-07\",\n" +
//				"\t\t\"conEndTime\":\"2024-12-16\",\n" +
//				"\t\t\"remark\":\"181111\"\n" +
//				"\t},\n" +
//				"\t{\n" +
//				"\n" +
//				"\t\t\"userOwnId\":\"0002\",\n" +
//				"\t\t\"userType\":\"pl\",\n" +
//				"\t\t\"controlPlan\":\"方案2\",\n" +
//				"\t\t\"updateTime\":\"2024-12-07 15:20:12\",\n" +
//				"\t\t\"conStartTime\":\"2024-12-08\",\n" +
//				"\t\t\"conEndTime\":\"2024-12-17\",\n" +
//				"\t\t\"remark\":\"1896\"\n" +
//				"\t}\n" +
//				"]\n";
//		String lpggasdata = "[\n" +
//				"\t{\n" +
//				"\n" +
//				"\t\t\"hiddenTitle\":\"隐患1\",\n" +
//				"\t\t\"hiddenContent\":\"内容1\",\n" +
//				"\t\t\"hiddenLocation\":\"位置1\",\n" +
//				"\t\t\"source\":\"1\",\n" +
//				"\t\t\"longitude\":\"120.0000\",\n" +
//				"\t\t\"latitude\":\"80.0000\",\n" +
//				"\t\t\"hiddenType\":\"1\",\n" +
//				"\t\t\"hiddenFindPeople\":\"1的\",\n" +
//				"\t\t\"hiddenFindDate\":\"2024-12-07 15:20:11\",\n" +
//				"\t\t\"dealResult\":\"结果1\",\n" +
//				"\t\t\"remediation\":\"1\",\n" +
//				"\t\t\"remark\":\"181111\"\n" +
//				"\t},\n" +
//				"\t{\n" +
//				"\n" +
//				"\t\t\"hiddenTitle\":\"隐患2\",\n" +
//				"\t\t\"hiddenContent\":\"内容2\",\n" +
//				"\t\t\"hiddenLocation\":\"位置2\",\n" +
//				"\t\t\"source\":\"2\",\n" +
//				"\t\t\"longitude\":\"120.0000\",\n" +
//				"\t\t\"latitude\":\"80.0000\",\n" +
//				"\t\t\"hiddenType\":\"2\",\n" +
//				"\t\t\"hiddenFindPeople\":\"2的\",\n" +
//				"\t\t\"hiddenFindDate\":\"2024-12-07 15:20:11\",\n" +
//				"\t\t\"dealResult\":\"结果2\",\n" +
//				"\t\t\"remediation\":\"2\",\n" +
//				"\t\t\"remark\":\"1811112\"\n" +
//				"\t}\n" +
//				"]\n";
		String lpggasdata = "[\n" +
				"\t{\n" +
				"\n" +
				"\t\t\"annual\":\"2025\",\n" +
				"\t\t\"securityCodeIn\":\"2025091552468\",\n" +
				"\t\t\"securityCode\":\"2025-01\",\n" +
				"\t\t\"userOwnId\":\"0001\",\n" +
				"\t\t\"userName\":\"张晓丽\",\n" +
				"\t\t\"indoorTime\":\"2024-12-07 15:20:11\",\n" +
				"\t\t\"indoorStatus\":\"NONE\",\n" +
				"\t\t\"houseNumber\":\"\",\n" +
				"\t\t\"phoneNumber\":\"18630055555\"\n" +
				"\t},\n" +
				"\t{\n" +
				"\n" +
				"\t\t\"annual\":\"2025\",\n" +
				"\t\t\"securityCodeIn\":\"202509155254\",\n" +
				"\t\t\"securityCode\":\"2025-01\",\n" +
				"\t\t\"userOwnId\":\"0002\",\n" +
				"\t\t\"userName\":\"李丽\",\n" +
				"\t\t\"indoorTime\":\"2024-12-07 15:20:21\",\n" +
				"\t\t\"indoorStatus\":\"DONE\",\n" +
				"\t\t\"houseNumber\":\"\",\n" +
				"\t\t\"phoneNumber\":\"18630055665\"\n" +
				"\t}\n" +
				"]\n";
		JSONArray objects = JSON.parseArray(lpggasdata);

		System.out.println(objects);
		System.out.println(DESEncoder.encrypt(objects.toString(),"GCQYTRQ00001"));



	}

}
