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 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" +
				"\t\t\"userOwnId\":\"用户自有编号-接口来的-01\",\n" +
				"\t\t\"nickName\":\"用户名称-接口来的-01\",\n" +
				"\t\t\"userType\":\"1\",\n" +
				"\t\t\"villageId\":\"3\",\n" +
				"\t\t\"address\":\"地址-接口来的-01\",\n" +
				"\t\t\"longitude\":\"14.56\",\n" +
				"\t\t\"latitude\":\"18.96\",\n" +
				"\t\t\"linkman\":\"联系人-接口来的-01\",\n" +
				"\t\t\"phone\":\"电话-接口来的-01\",\n" +
				"\t\t\"remarks\":\"备注-接口来的-01\",\n" +
				"\t\t\"safetyDeviceList\":[\n" +
				"\t\t\t{\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-01-01\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-01-01\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-01-01\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-01-01\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-01-01\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-20\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-01-01\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-01-01\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-01-01\"\n" +
				"\t\t\t},\n" +
				"\t\t\t{\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-01-02\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-01-02\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-01-02\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-01-02\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-01-02\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-20\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-01-02\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-01-02\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-01-02\"\n" +
				"\t\t\t},\n" +
				"\t\t\t{\n" +
				"\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-01-03\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-01-03\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-01-03\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-01-03\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-01-03\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-20\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-01-03\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-01-03\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-01-03\"\n" +
				"\t\t\t}\n" +
				"\t\t]\n" +
				"\t},\n" +
				"\t{\n" +
				"\n" +
				"\t\t\"userOwnId\":\"用户自有编号-接口来的-02\",\n" +
				"\t\t\"nickName\":\"用户名称-接口来的-02\",\n" +
				"\t\t\"userType\":\"2\",\n" +
				"\t\t\"villageId\":\"\",\n" +
				"\t\t\"address\":\"地址-接口来的-02\",\n" +
				"\t\t\"longitude\":\"14.56\",\n" +
				"\t\t\"latitude\":\"18.96\",\n" +
				"\t\t\"linkman\":\"联系人-接口来的-02\",\n" +
				"\t\t\"phone\":\"电话-接口来的-02\",\n" +
				"\t\t\"remarks\":\"备注-接口来的-02\",\n" +
				"\t\t\"safetyDeviceList\":[\n" +
				"\t\t\t{\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-02-01\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-02-01\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-02-01\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-02-01\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-02-01\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-21\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-02-01\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-02-01\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-02-01\"\n" +
				"\t\t\t},\n" +
				"\t\t\t{\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-02-02\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-02-02\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-02-02\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-02-02\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-02-02\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-21\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-02-02\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-02-02\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-02-02\"\n" +
				"\t\t\t},\n" +
				"\t\t\t{\n" +
				"\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-02-03\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-02-03\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-02-03\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-02-03\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-02-03\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-21\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-02-03\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-02-03\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-02-03\"\n" +
				"\t\t\t}\n" +
				"\t\t]\n" +
				"\t},\n" +
				"\t{\n" +
				"\n" +
				"\t\t\"userOwnId\":\"用户自有编号-接口来的-03\",\n" +
				"\t\t\"nickName\":\"用户名称-接口来的-03\",\n" +
				"\t\t\"userType\":\"4\",\n" +
				"\t\t\"villageId\":\"\",\n" +
				"\t\t\"address\":\"地址-接口来的-03\",\n" +
				"\t\t\"longitude\":\"14.56\",\n" +
				"\t\t\"latitude\":\"18.96\",\n" +
				"\t\t\"linkman\":\"联系人-接口来的-03\",\n" +
				"\t\t\"phone\":\"电话-接口来的-03\",\n" +
				"\t\t\"remarks\":\"备注-接口来的-03\",\n" +
				"\t\t\"safetyDeviceList\":[\n" +
				"\t\t\t{\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-03-01\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-03-01\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-03-01\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-03-01\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-03-01\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-22\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-03-01\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-03-01\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-03-01\"\n" +
				"\t\t\t},\n" +
				"\t\t\t{\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-03-02\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-03-02\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-03-02\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-03-02\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-03-02\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-22\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-03-02\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-03-02\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-03-02\"\n" +
				"\t\t\t},\n" +
				"\t\t\t{\n" +
				"\n" +
				"\t\t\t\t\"fDeviceName\":\"设备名称-接口来的-03-03\",\n" +
				"\t\t\t\t\"fDeviceModel\":\"设备型号-接口来的-03-03\",\n" +
				"\t\t\t\t\"fIotNo\":\"物联网编号-接口来的-03-03\",\n" +
				"\t\t\t\t\"fRelationDeviceType\":\"设备类型-接口来的-03-03\",\n" +
				"\t\t\t\t\"fDetectionMedium\":\"探测介质-接口来的-03-03\",\n" +
				"\t\t\t\t\"fDeviceInstallTime\":\"2024-09-22\",\n" +
				"\t\t\t\t\"fDeviceInstallPosition\":\"设备安装位置-接口来的-03-03\",\n" +
				"\t\t\t\t\"fHead\":\"负责人-接口来的-03-03\",\n" +
				"\t\t\t\t\"fPhone\":\"联系电话-接口来的-03-03\"\n" +
				"\t\t\t}\n" +
				"\t\t]\n" +
				"\t}\n" +
				"]\n";
		JSONArray objects = JSON.parseArray(lpggasdata);

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



	}

}
