package com.cirle.scientific.utils;

import java.security.Security;

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

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
 
 
public class AESpkcs7paddingUtil {
 
	/**
	 * 密钥算法
	 */
	private static final String ALGORITHM = "AES";
	/**
	 * 加解密算法/工作模式/填充方式
	 */
	private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";
	
	/**
	 * AES加密
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String encryptData(String data, String key) throws Exception {
		Security.addProvider(new BouncyCastleProvider());
		// 创建密码器
		Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING, "BC");
		
		// 生成key
		SecretKeySpec secretkey = new SecretKeySpec(Md5Util.encode(key).toLowerCase().getBytes(), ALGORITHM);
		// 初始化
		cipher.init(Cipher.ENCRYPT_MODE, secretkey);
		return new String(Base64.encode(cipher.doFinal(data.getBytes())));
	}
 
	/**
	 * AES解密
	 * 
	 * @param base64Data 
	 * @return
	 * @throws Exception
	 */
	public static String decryptData(String base64Data, String key) throws Exception {
		Security.addProvider(new BouncyCastleProvider());
		//生成key
		SecretKeySpec secretkey = new SecretKeySpec(Md5Util.encode(key).toLowerCase().getBytes(), ALGORITHM);
		
		Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING, "BC");
		
		cipher.init(Cipher.DECRYPT_MODE, secretkey);
		
		return new String(cipher.doFinal(Base64.decode(base64Data)));
	}
 
	public static void main(String[] args) throws Exception {


		
		String encryptedData = "XHMBZsRqRPLnASroAXi0MrgSO/EZkmZrr3pZH/V+YFUbVCxWI3tGIsoDymBvQmbU0jlPdqmj+1gEhjfm7K95mJQDj4yVqAEFxilJEEDeLZMHc0RNS5veZ0cLWhBwOaD5SdDHGqYBWLpWtw3Zlu8XsZ0Qpkp9zRUnwN40J/ahd3G6kDCwWZ1meg6rcjlaSj6OqE4LYa7YulwXrAxWJdlHN3v+ZcANeDjoNdZM270dd2P/brya1rPOwrdqglVigH7sY0E9Qw270UDYUC6mcfhWRrhOMx24f2wpUEXlsJ5T954j5PoUCgSc25HBP5RAVfPirqCET2UMm8egEcjk6GO+B623TvwL+OyXPkVyniG5Ha9hG6tihy9QbwF+/DYR+KN3/UMaRoL8X2btApOVxgwi7CivS+481uTFkvFVyqoV7BMsTJNVAxMFQneMcfABf2qIbZuQXr8qnZ1mPyP7SOa9HLMfgZ93SDVmMxFHBViPOS+lHCXuJVNPGQ5R3fJEBiLVYk5ZffKmkn0Bc8j06O6HFOIDirlNqvZv73VaXMNC5YNfq/VtVJ+Bo1IVVX65p7T26WwcIUFDrcWsNlKaNpIMZXlXRGbiAlE0QXlvmO2kPsBZAZcLtlSmnkStVlw3PSIhc2ISyht34JKDoq+WCfeMRQeJVa8OQiX8izLFC1fi1HcEdpieTUwvtsI0YDx5Q1HbQAbldglVfpRRk7JJQng0dGmEq8XCmaxBF2FY3I8VkVV9xt+Su6+fQn7T3WTr4WxSox6dgMr6WGpMhOlcoJWov6GeaDIb2ZPpfAC5i+9OGlgAC6X0Sod05fEF1LqYf3B4jp0qBmw9w7XyKgX9ABOifzfmnv9bkwJDKZ/VYkmeAvUdQyUK97vlu/l8L6Txtowf5jWZNxOqCWuXKgLpxAFAbw3TqcDnIvgUN21XGcIIpV+1NTU+EcndlOjkwA4YI/KYy6u9Z5snV4WQE/ToL9p/Y9i1+sKXYtfJz8oaS8mucX2YqNtuPMbkKIn6veS1ECwrITwZoMuC/HNsVSJtNB1gvBPa0RI5+8ftae9AP1fzrRnEmpdqHdwPSJR1nH66StQq";
		String sessionKey = "Mevos2017PaySecretMevosPaySecret";
		String requestBody = AESpkcs7paddingUtil.decryptData(encryptedData, sessionKey);
    	String out_trade_no = XmlUtil.readXMLStr(requestBody, "out_trade_no");
    	String success_time = XmlUtil.readXMLStr(requestBody, "success_time");
		System.out.println(out_trade_no+"**"+success_time);
	
	}
 
}

