package com.kmall.manager.manager.wechat;

import com.kmall.manager.manager.pay.wxpay.CommonWxPayPropertiesBuilder;
import com.kmall.common.utils.wechat.Base64Util;
import com.kmall.common.utils.wechat.MD5Util;
import com.kmall.common.utils.wechat.WechatRefundApiResult;
import com.kmall.common.utils.XmlUtil;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
 * @author huangyaqin
 * @version 1.0
 * 2018-11-01 14:42
 */

public class AESUtil {

    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "AES";
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS5Padding";
    /**
     * 生成key
     */
//    private static SecretKeySpec key = new SecretKeySpec(MD5Util.MD5Encode("U5XYsGSLMhhNFDBUWfgPGLsF3rG7GTXq", "UTF-8").toLowerCase().getBytes(), ALGORITHM);

    private static SecretKeySpec key = new SecretKeySpec(MD5Util.MD5Encode(CommonWxPayPropertiesBuilder.instance().getPaySignKey(), "UTF-8").toLowerCase().getBytes(), ALGORITHM);

    /**
     * AES加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptData(String data) throws Exception {
        // 创建密码器
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        // 初始化
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64Util.encode(cipher.doFinal(data.getBytes()));
    }

    /**
     * AES解密
     *
     * @param base64Data
     * @return
     * @throws Exception
     */
    public static String decryptData(String base64Data) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(Base64Util.decode(base64Data)));
    }

    public static void main(String[] args) throws Exception {
        String A = "h9k5zhC2Fmjf9v/zWCmc1WIHzEJmpvEhFswYUcvsJGHVAQjgBUl0tnGkKvFY+0vxW1lviCEw14pia5EW1+SyT+DCFGjDe+w+Tj9ZVRploWmcmzz+pb6GJOKoICQQagzbqDTvEFMjmy9OZNbiDgx9061cMu6jPHRu/pQXvCPxQ6vSvbTdHop8AbnizmHRb9lKj4lP3q8Ah0O7Kja0kPgwsn1d9Y+RHTOZJgGC4Mm3oCAq0c8Ar8yxA97epVhc5VYSSyEcKem87IJke2nUVMYMQskMo1cCqGgTKTrmG7OMcmvBIMrdVh5FxWVvAkIzCbRo85oaUDj0uSc7ulyVLCO9KDYl0QHd7AoreY5AcRkfJTWG2i90rXXE2nQ7l3hFmOEAvLR7v4Q6mM85KQOxDpw9IS4lGXTPcKtDjYPsU1Vyt/SidvUZjzmDr4bpmRpcTtgY7Sg4FfCE908Q5+u3IrtYIBstid9WJ4yH1nsG3ADj722E6FezJF8cEaT8n7SITIaCBrxFbCrEUf56oV0puoKGljTlCuV9AH1WB1aUckaK/ix7Ua6xgY/MOAAgomV8ptKaU9MUlL3C1oVOlUXQKxJyqxtjBJTkBr9am9F7hizFW69fO0tiw8LEt8MkAOvgFrLq7XM8eNocO3++ZjsWL9CEkhm7taQnrl34IBi2wqBfjzV/mGqt56q83Aji0mD8/XkMpt7V1ZhD8hb9FnCs6dueWyLgOA+71I2ckBJsUN2JKGJE/N8+SHFfFMoyTOTcH/sWYBAt313j4i1D93WzMyGv9dy82lgHl9Q/yXwMQBVqI1ORQ/aVJWqJaf+pgyOp6uWolgyFsDrMg3Wio1Cav6qXTcZDbdzMP0/I5nUSp94+JZQQv52S6EP56r6FdN3s9NFCOVgkxGr+097AxKSh4S6BD9TlBZUfg/tKVp19/0cjurquqq510nrEwWEefIaSrtRdE519py/Q9wPbk4fDQ1iujq+fEKkqt0AuQ+ErDfkXAAPjCyLXpMD3XIv3tyqyDmk75j5BUZdrVCUk4sy4Tz1lVxpSw9F1u6eCMOVKUilJNc4=";
        String B = AESUtil.decryptData(A);
        WechatRefundApiResult refundApiResult = (WechatRefundApiResult) XmlUtil.xmlStrToBean(B, WechatRefundApiResult.class);
        System.out.println(B);
        System.out.println(refundApiResult);
    }

}
