package com.jeequan.jeepay.thirdparty.channel.yspay.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.jeequan.jeepay.core.exception.BizException;

import java.nio.charset.StandardCharsets;

public class AESUtil {

	/**
	 * 加密
	 *
	 * @param content 需要加密的内容
	 * @param password  加密密钥
	 */
	public static byte[] encrypt(byte[] content, byte[] password) {
		AES aes = SecureUtil.aes(password);
		return aes.encrypt(content);
	}


	/**解密
	 * @param content  待解密内容
	 * @param password 解密密钥
	 */
	public static byte[] decrypt(byte[] content, byte[] password) {
		AES aes = SecureUtil.aes(password);
		return aes.decrypt(content);
	}


	public static void main(String[] args) {

		try {
			String content = "IgVXDyReWEj4qOEtoVWf7K36a+SpRe8zmbRbouki/kcJGSoRYqmhznvA7nF86DEIfB+XYUX6vLRyMP\n" +
					"4CoqPiWHGW3WOquAUF1QlEH6ZR9HqDEucT65lqhMzAmk6Sxv6TRLySWjffjmN0VS1iIfyNF5+jR9L\n" +
					"mFjrY6neoEErUHjlsyZBqLlDDtYFy4hlLSGTfFLNnus/WfDaAmevq7tBwlJUBkRX2MU9FlQ2YzOHpQ36\n" +
					"9mNOAspbIiWgA5MQXlCqjdiOq22q653udLhfwOhA3+TKUEju4pS05EYG3JQtmN8aMQtCj6xJCQPP\n" +
					"/YAp+AIpJ1dqIAdqvoSKGUQEWYLNaGJsnBrq2yS/PeyNYGBSzic5QTb+nsZVrUB+wLp68qjZUtqr/cvc\n" +
					"pwB1I4kKHrfS45SCkJ8PtSeIUHdOE+m+F2sSET2KBB2nQHsIEy/IVJ1xXI2LDgPhBtnQWLirt6jzIOFy5x\n" +
					"XltaUou5hp0NPmMR+LzyFYB86QN3v7Hcr4rt0zVN/aoBo2BuSWu5cFkiBJrOPu34N5VSZRznqL/asO\n" +
					"SqgLxWlVKiGyZnrJp/HqLQ5QD";

			String password = "0123456789abcdef";
			System.out.println(decryptStrAES(content, password));
		}catch (Exception e) {
			throw new BizException(e.getMessage());
		}
	}

	public static String decryptStrAES(String text, String key){
		AES aes = SecureUtil.aes(key.getBytes(StandardCharsets.UTF_8));
		return aes.decryptStr(text, StandardCharsets.UTF_8);
	}
}
