package com.palmnest.webapp.controller.weixiao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @Author hxt
 * @Description 加密工具类
 * @Date 2018/12/25 10:31
 **/
public class AES {

	public static void main(String args[]) throws Exception {
		// app_key
		String cKey = "09C93225D384C86F";
		// 需要加密的字串
		String cSrc  = "[{\"card_number\":\"00000001\",\"gender\":\"男\",\"identity_type\":\"员工\",\"name\":\"笑天\",\"organization\":\"服务之窗运营中心/中航中心/测试飞亚达企业/信息管理部\",\"telephone\":\"17691355199\"}]";
		String cSrc1 = "[{\"card_number\":\"000827\",\"name\":\"罗健2\",\"gender\":\"男\",\"identity_type\":\"员工\",\"telephone\":\"15889793363\",\"organization\":\"前海超级前台(深圳)信息技术有限公司/航空大厦/服务之窗/研发部\"}]\n";
		// app_secret 取前16位;
		String cIv = "B2622F07C27972218135C83A39B92F4D";
		System.out.println(cSrc);

		JSONArray  jsonArray  = new JSONArray();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", "笑天");
		jsonObject.put("telephone", "17691355199");
		jsonObject.put("organization", "服务之窗运营中心/中航中心/测试飞亚达企业/信息管理部");
		jsonObject.put("gender", "男");
		jsonObject.put("identity_type", "员工");
		jsonObject.put("card_number", "00000001");
		jsonArray.add(jsonObject);

		// 加密
		long lStart = System.currentTimeMillis();
//        String enString = AES.Encrypt(jsonArray.toString(), cKey, cIv.substring(0,16));
		String enStr = AES.Encrypt("[{\"card_number\":\"00000001\",\"gender\":\"男\",\"identity_type\":\"员工\",\"name\":\"笑天\",\"organization\":\"服务之窗运营中心/中航中心/测试飞亚达企业/信息管理部\",\"telephone\":\"17691355199\"}]", "09C93225D384C86F", "B2622F07C2797221");

		System.out.println("加密后的字串是：" + enStr);
//        System.out.println("加密后的字串是：" + enString);
		long lUseTime = System.currentTimeMillis() - lStart;
		System.out.println("加密耗时：" + lUseTime + "毫秒");

		JSONObject object = new JSONObject();
//        object.put("raw_data",enString);
		object.put("app_key", cKey);

//        String result = HttpKit.HttpClientPost(object.toString(), "https://office.haina.com/open/identity/sync-stu-info");
//        System.out.println(object.toString());
//        System.out.println(URLDecoder.decode(result, "Unicode"));
//        // 解密
//        lStart = System.currentTimeMillis();
//        String DeString = AES.Decrypt("5e690e9837eed27f779eb982d63a60af017c7615206397b38abba84d580354e5a7e19251356bb3f444b34ba9d362de7437b9c1e3131f3a21f796e6241fe04ceddaffa638bae0b5d5d49754961736e6e94444df299e473ea59d43c6bc1e10233c8a176425dc20325aec775172f4cd0a72b9b86cf9c0a032f4267cd2d6cc5dbfff0a45ebcadfbba9c872d80c36a33dc8e0523591a47b235fabd3923ebb6e1d46aee64b71722ef1dd5d2a11898376c1f76f777b8219b703d93aec80e7cfe0af89e3ff3b81f00c6bbb42dbc3ed4ed6e9251c316064ba61deaed1bcc7f3abcd919a910a2d2e09708d0fdbadadbe3ad09c56510b04909163343e5acaca4c20739a9dd27701cde81be0dce579724c86a675927755cf184a59db5eb8e93732f93a80d8beefb3d9bb412a6ed717b9d8877aa128b590dc7008e7bccf548a1f2100e8ef86c9f17c840ab1c068980fb1a8e48df94735369febfd4ea1a94e1edf6e622fe3756726db9d22ad875aef771b97fe88445ecc17573bddd33a9612bc349bdb806292e59febe09b4ffd03286b8812e3e648e22ca0c2d13d1b53af197b13aa95a02698177daadf2825dd4ce82b66beb0f7bd00090318f82b4197c4d24873a0ccfd7c437238bfff22b6e86f8772a2169f90615e7629fd66453fddf38913adaa8d78b54f481363873db6f17fb15e05067c86ff9dd7157e021a09d56cc8607533551d0bc9fc9ffdbd0343bd4e58712350499f59bc32fade6c73c7964623911edbbf5700ec2d", cKey, cIv.substring(0,16));
//        System.out.println("解密后的字串是：" + DeString);
//        lUseTime = System.currentTimeMillis() - lStart;
//        System.out.println("解密耗时：" + lUseTime + "毫秒");


	}

	public static String Encrypt(String sSrc, String sKey, String sIv) throws Exception {

		Cipher cipher    = Cipher.getInstance("AES/CBC/NoPadding");
		int    blockSize = cipher.getBlockSize();

		byte[] dataBytes       = sSrc.getBytes("UTF-8");
		int    plaintextLength = dataBytes.length;
		if (plaintextLength % blockSize != 0) {
			plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
		}

		byte[] plaintext = new byte[plaintextLength];
		System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

		SecretKeySpec   keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
		IvParameterSpec ivspec  = new IvParameterSpec(sIv.getBytes());

		cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
		byte[] encrypted = cipher.doFinal(plaintext);

		return byte2hex(encrypted).toLowerCase();
	}

	public static String Decrypt(String sSrc, String sKey, String sIv) throws Exception {

		byte[] encrypted1 = hex2byte(sSrc);

		Cipher          cipher  = Cipher.getInstance("AES/CBC/NoPadding");
		SecretKeySpec   keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
		IvParameterSpec ivspec  = new IvParameterSpec(sIv.getBytes());

		cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

		byte[] original       = cipher.doFinal(encrypted1);
		String originalString = new String(original);

		return originalString;
	}

	public static byte[] hex2byte(String strhex) {
		if (strhex == null) {
			return null;
		}
		int l = strhex.length();
		if (l % 2 == 1) {
			return null;
		}
		byte[] b = new byte[l / 2];
		for (int i = 0; i != l / 2; i++) {
			b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
					16);
		}

		return b;
	}

	public static String byte2hex(byte[] b) {
		String hs   = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
		}

		return hs.toUpperCase();
	}
}
