package com.bonus.boot.manager.basic.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

/**
 *
 * AES加密工具类
 */
@Slf4j
public class AESCBCUtils {
		//使用AES-256-CBC加密模式，key需要为16位,key和iv可以相同！
		/**
		 * 密钥算法
		 */
		private static final String KEY_ALGORITHM = "AES";

		/**
		 * 加密/解密算法 / 工作模式 / 填充方式
		 * Java 6支持PKCS5Padding填充方式
		 * Bouncy Castle支持PKCS7Padding填充方式
		 */
		private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding";

		/**
		 * 偏移量，只有CBC模式才需要
		 */
		private final static String ivParameter = "1234567812345678";


		/**
		 * AES要求密钥长度为128位或192位或256位，java默认限制AES密钥长度最多128位
		 */
		public static String sKey = "zhst@bonus@zhst@bonus@1234567890";

		/**
		 * 编码格式
		 */
		public static final String ENCODING = "utf-8";

		static {
			//如果是PKCS7Padding填充方式，则必须加上下面这行
			Security.addProvider(new BouncyCastleProvider());
		}

		/**
		 * AES加密
		 *（CBC模式）
		 * @param source 源字符串
		 * @param
		 * @throws Exception
		 * @return 加密后的密文
		 */
		public static String encrypt(String source, String key) throws Exception {
			byte[] sourceBytes = source.getBytes(ENCODING);
			byte[] keyBytes = key.getBytes(ENCODING);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes(ENCODING));
			cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM), iv);
			byte[] decrypted = cipher.doFinal(sourceBytes);
			return Base64.encodeBase64String(decrypted);
		}

		/**
		 * AES解密
		 *（CBC模式）
		 * @param encryptStr 加密后的密文
		 * @param
		 * @throws Exception
		 * @return 源字符串
		 */
		public static String decrypt(String encryptStr)  {
			try{
				String key=sKey;
				byte[] sourceBytes = Base64.decodeBase64(encryptStr);
				byte[] keyBytes = key.getBytes(ENCODING);
				Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
				IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes(ENCODING));
				cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM), iv);
				byte[] decoded = cipher.doFinal(sourceBytes);
				return new String(decoded, ENCODING);
			}catch (Exception e){
				log.error(e.toString(),e);
			}
			return  "";
		}


		public static void main(String[] args) throws Exception {
			/*String key = "1234567812345678";
			// 加密
			long lStart = System.currentTimeMillis();
			String enString = AESCBCUtils.encrypt("2023-06-06", AESCBCUtils.sKey);
			System.out.println("加密后的字串是：" + enString);
			 enString = AESCBCUtils.encrypt("Bonus@admin123", AESCBCUtils.sKey);
			System.out.println("加密后的字串是：" + enString);
			long lUseTime = System.currentTimeMillis() - lStart;
			System.out.println("加密耗时：" + lUseTime + "毫秒");

			// 解密
			lStart = System.currentTimeMillis();
			String DeString = AESCBCUtils.decrypt("[encoded data, 5356 characters]");
			System.out.println("解密后的字串是：" + DeString);
			lUseTime = System.currentTimeMillis() - lStart;
			System.out.println("解密耗时：" + lUseTime + "毫秒");*/
			String key = "1234567812345678";
			// 加密
//			long lStart = System.currentTimeMillis();
//			String enString = AESCBCUtils.encrypt("15955081965", AESCBCUtils.sKey);
//			String enString2 = AESCBCUtils.encrypt("ccw1998@YYT", AESCBCUtils.sKey);
//			String enString3 = AESCBCUtils.encrypt("1", AESCBCUtils.sKey);
//			String enString4= AESCBCUtils.encrypt("10", AESCBCUtils.sKey);
//			System.out.println("加密后的字串是：" + enString);
//			System.out.println("加密后的字串是：" + enString2);
//			System.out.println("加密后的字串是：" + enString3);
//			System.out.println("加密后的字串是：" + enString4);
//			System.err.println(AESCBCUtils.decrypt(enString));;
			String jsonStr = "[{'userName':'赵六','phone':'15955081966','systemName':'实名制管理系统','auth':'移动端','isBlacklist':'0','accountStatus':'1'},{'userName':'李琦','phone':'15955081967','systemName':'实名制管理系统','auth':'后台','isBlacklist':'0','accountStatus':'1'}]";
			String jsonStr2 = "{'userName':'赵六','phone':'15955081966','systemName':'实名制管理系统','auth':'后台','isBlacklist':'0','accountStatus':'1'}";
			String jsonStr3 = "{'userName':'赵六','phone':'15955081966','systemName':'实名制管理系统'}";
			String jsonStr4 = "[{'userName':'张三','appId':'实名制管理系统','model':'系统设置>>系统登录日志','operFun':'查询','description':'查询系统登录日志','createDate':'2023-09-14 12:00:00','result':'200'}]";
			String jsonStr5 = "[{'loginName':'15955081965','userName':'李四','loginIp':'192.168.0.1','loginUrl':'实名制管理系统','loginTime':'2023-09-14 12:00:00','loginStatus':'1'}]";
			String str = AESCBCUtils.encrypt(jsonStr, AESCBCUtils.sKey);
			String str2 = AESCBCUtils.encrypt(jsonStr2, AESCBCUtils.sKey);
			String str3 = AESCBCUtils.encrypt(jsonStr3, AESCBCUtils.sKey);
			String str4 = AESCBCUtils.encrypt(jsonStr4, AESCBCUtils.sKey);
			String str5 = AESCBCUtils.encrypt(jsonStr5, AESCBCUtils.sKey);
			System.err.println(str);
			System.err.println(str2);
			System.err.println(str3);
			System.err.println(str4);
			System.err.println(str5);
		}
}
