package com.post;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class AesUtil {

	/**
	 * 调用者持有的KEY加密和解密的必选参数，可以写到配置文件中
	 */
	public static final String aesKey = "4951FAD78B032EC6";

	public static final String VIPARA = "71652BEC09A8FD34";
	public static final String bm = "utf-8";

	/**
	 * 字节数组转化为大写16进制字符串
	 *
	 * @param b
	 * @return
	 */
	private static String byte2HexStr(byte[] b) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < b.length; i++) {
			String s = Integer.toHexString(b[i] & 0xFF);
			if (s.length() == 1) {
				sb.append("0");
			}

			sb.append(s.toUpperCase());
		}

		return sb.toString();
	}

	/**
	 * 16进制字符串转字节数组
	 *
	 * @param s
	 * @return
	 */
	private static byte[] str2ByteArray(String s) {
		int byteArrayLength = s.length() / 2;
		byte[] b = new byte[byteArrayLength];
		for (int i = 0; i < byteArrayLength; i++) {
			byte b0 = (byte) Integer.valueOf(s.substring(i * 2, i * 2 + 2), 16).intValue();
			b[i] = b0;
		}

		return b;
	}

	/**
	 * AES 加密
	 *
	 * @param content
	 *            明文
	 * @param password
	 *            生成秘钥的关键字
	 * @return
	 */

	public static String aesEncrypt(String content, String password) {
		try {
			IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
			SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
			byte[] encryptedData = cipher.doFinal(content.getBytes(bm));

			return byte2HexStr(encryptedData);
		} catch (NoSuchAlgorithmException e) {
			log.error("【加密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			log.error("【加密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			log.error("【加密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			log.error("【加密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			log.error("【加密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (BadPaddingException e) {
			log.error("【加密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			log.error("【加密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			log.error("【加密】失败：" + e.getMessage());
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * AES 解密
	 *
	 * @param content
	 *            密文
	 * @param password
	 *            生成秘钥的关键字
	 * @return
	 */

	public static String aesDecrypt(String content, String password) {
		try {
			// byte[] byteMi = Base64.decode(content);
			byte[] byteMi = str2ByteArray(content);
			IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
			SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
			byte[] decryptedData = cipher.doFinal(byteMi);
			return new String(decryptedData, "utf-8");
		} catch (NoSuchAlgorithmException e) {
			log.error("【解密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			log.error("【解密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			log.error("【解密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			log.error("【解密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (BadPaddingException e) {
			log.error("【解密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			log.error("【解密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			log.error("【解密】失败：" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			log.error("【解密】失败：" + e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		//POS_LOCK_KEY=4951FAD78B032EC6

//		String card_no = "E46211673";
		String card_no = "6";
		// String card_no = "替换笑脸";
		String temp_cardno = AesUtil.aesEncrypt(card_no, AesUtil.aesKey);//加密
		System.out.println(temp_cardno);
		temp_cardno = AesUtil.aesDecrypt(temp_cardno, AesUtil.aesKey);//解密
		System.out.println(temp_cardno);
	}
}
