package com.xm.demo.util.aes;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

/**
 * @ClassName: AESUtil
 * @Description: AES对称算法加解密工具类
 * @author mengxc
 * @date 2021年9月1日 下午3:28:49
 */
public class AESUtil {

	private static final int KEY_LENGTH = 128;
	private static final String CHARSETNAME = "UTF-8";
	private static final String KEY_ALGORITHM = "AES";
	private static final String RNG_ALGORITHM = "SHA1PRNG";
	private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

	/**
	 * 生成随机密钥
	 * 
	 * @return
	 */
	public static String generateKey() {
		KeyGenerator keyGen;
		try {
			keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);
			keyGen.init(KEY_LENGTH);
			// 要生成多少位,只需要修改这里即可128, 192或256
			SecretKey sk = keyGen.generateKey();
			byte[] bytes = sk.getEncoded();
			return parseByte2HexStr(bytes);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * AES解密
	 * 
	 * @param content 待解密内容
	 * @param aesKey  密钥
	 * @return
	 */
	public static String aesDecrypt(String content, String aesKey) {
		try {
			byte[] byteContent = parseHexStr2Byte(content);
			if (byteContent == null) {
				return null;
			}
			SecretKey key = generateKey(aesKey);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			return new String(cipher.doFinal(byteContent)); // 解密
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取秘密密钥
	 * 
	 * @return
	 * @throws Exception
	 */
	private static SecretKey generateKey(String seed) throws Exception {
		SecureRandom secureRandom = SecureRandom.getInstance(RNG_ALGORITHM);
		secureRandom.setSeed(seed.getBytes(CHARSETNAME));
		KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
		kgen.init(KEY_LENGTH, secureRandom);
		return kgen.generateKey();
	}

	/**
	 * AES加密
	 * 
	 * @param content 待加密内容
	 * @param aesKey  密钥
	 * @return
	 */
	public static String aesEncrypt(String content, String aesKey) {
		try {
			byte[] byteContent = content.getBytes(CHARSETNAME);
			SecretKey key = generateKey(aesKey);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);// 创建密码器
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
			return parseByte2HexStr(cipher.doFinal(byteContent));// 加密
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

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

	/**
	 * 将16进制转换为二进制数组
	 * 
	 * @param hexStr
	 * @return
	 */
	private static byte[] parseHexStr2Byte(String hexStr) {
		try {
			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;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}