微信支付退款结果通知(回调) AES-256-ECB解密 代码

微信支付中的退款后,微信那边需要回调。恶心的是需要我们解密。

给大家我的代码看看。

每个类都有一个主方法供大家测试用。

Base64Util
package com.xz.app.util;

import java.util.Base64;

/**
 *


import java.util.Base64;

/**
 * @author jing.huang
 * @function jdk8支持
 * @date 2018年1月10日
 * @version
 */
public class Base64Util {


    public static byte[] decode(String encodedText){
        final Base64.Decoder decoder = Base64.getDecoder();
        return decoder.decode(encodedText);
    }

    public static String encode(byte[] data){
        final Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(data);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            final Base64.Decoder decoder = Base64.getDecoder();
            final Base64.Encoder encoder = Base64.getEncoder();
            final String text = "字串文字";
            final byte[] textByte = text.getBytes("UTF-8");
            //编码
            final String encodedText = encoder.encodeToString(textByte);
            System.out.println(encodedText);
            //解码
            System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }

//        final Base64.Decoder decoder = Base64.getDecoder();
//        final Base64.Encoder encoder = Base64.getEncoder();
//        final String text = "字串文字";
//        final byte[] textByte = text.getBytes("UTF-8");
//        //编码
//        final String encodedText = encoder.encodeToString(textByte);
//        System.out.println(encodedText);
//        //解码
//        System.out.println(new String(decoder.decode(encodedText), "UTF-8"));

    }

}

 

MD5
package com.xz.app.util;



import java.security.MessageDigest;



public class MD5 {
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * 转换字节数组为16进制字串
     * @param b 字节数组
     * @return 16进制字串
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuilder resultSb = new StringBuilder();
        for (byte aB : b) {
            resultSb.append(byteToHexString(aB));
        }
        return resultSb.toString();
    }

    /**
     * 转换byte到16进制
     * @param b 要转换的byte
     * @return 16进制格式
     */
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    /**
     * MD5编码
     * @param origin 原始字符串
     * @return 经过MD5加密之后的结果
     */
    public static String MD5Encode(String origin) {
        String resultString = null;
        try {
            resultString = origin;
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    public static void main(String[] args) {
        System.out.println("111"+MD5Encode("111"));
    }

}

 

AESUtil
/**
 * 
 */
package encode;

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

/**
 * @author jing.huang
 * @function
 * @date 2018年1月10日
 * @version 
 */
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(MD5.MD5Encode("2IBtBXdrqC3kCBs4gaceL7nl2nnFadQv").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 req_info="Ih5osM/5IbPfHouVrUmwebd1yAW2Gys91jv006W1237sSi3z022Kx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jpg4SccA876HVy7J9P85NpdvCMNGInZ4fANDRE+YfZe4HeF+bbFj6JETcEFPpE9YW+bTbC0D+gl/otScJfvB2QUK7+EeBGPHN1EWX9zbr2Gw6AUaORdFk3mGxV5dtjuwWQrv5juzkXDs33Z2dUMslO+i3j0cTDHqwS4hptx2j6h2HvzgzltFbjo7nysU+4rArqJvrGW/9r18e1St9XgG21NALqixfaSmqetOR4zLVL4/+z3CEz8cg5r+/4GUOTf3XFmLCZ/wEkRQhKRNVibG0NFfiG3KnqArMJ/dheQHCd7qL+XX/ZV6tj8RLMgL7R6hOiR03Ljyikdxq9M3K9CTYgf03pHJd3geXX1LgXrLxc1flL6NW+zD3ZayGYpr1WpLsSMG7z8W5j1pme6cRj3n2+CwSFnOnOkxaFuLKoJAJIqM3gbC0eN++vY73RKphlI4zZqg6o5s3MXI6ju1yoi/ZQ+XbTg2JttsdbU0aySernKwkt0rYMf0j/Mcvo2axgHbI3w/iTm141WxHUjkQ+ga2X1pOWdGifGhSmMP8oGaA/WD5MAsK1qXX0eFvUWS/PTauCSTWq5Cmr8loA/KL3jgvB0nyR4mfccB+tPy4Ny7kzOlr/VNeb0ULf96R0AWFWCtdt8AmujAP0DYiM5FSmYLI0XRhpSDjnEbBM8+isNE1GlAVR3NzzemwQORihScovpAktbRSN/d3N+NgTjSoVDiJvCOxCs3thX9qt9iwYbA+/X/gv8lza2FZyIzwkQxGRcYl8JWKpXzNW8EWUNVnSLdHvQttDeV3CvgP/x579RGd6whyFYS6AaI0qw7oTjCFh2EHS/VzGvFuv166ZlVIJ4MNvg79O9h63ZOSE1LzVqEsVh8fDCfM2GgJ9aUdl95Djgunit4yIZOdoigR3f/BEHKrYCEham11rYohaAXs4XAXWihsV3WD5j4G/P+txvjAwujvf4HDwzHgFsmSml013U2mUiy+v4zw==";
        String B = AESUtil.decryptData(req_info);  
        System.out.println(B);  
               
        //加密
        String str = ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                ""+
                "";
        System.out.println(encryptData(str));
    }  
    
}

注意:key要改为自己的key

 

 

/**
	 * 
	 * 微信退款回调
	 * @param
	 * @param
	 */
	@ApiOperation(value = "微信退款回调", notes = "微信退款回调")
	@RequestMapping(value = "WXtrackRefunderrorrm2", method = RequestMethod.POST)
	@ResponseBody
	public void WXtrackRefunderrorrm2(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws  Exception{
		logger.info("开始回调");
		InputStream inStream = httpRequest.getInputStream();
		ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outSteam.write(buffer, 0, len);
		}
		String resultxml = new String(outSteam.toByteArray(), "utf-8");
		Map params = WXPayUtil.xmlToMap(resultxml);
		outSteam.close();
		inStream.close();
		logger.info("here");
		if (params.get("return_code").equals("SUCCESS")) {
			logger.info("退款成功0");
			logger.info("退款成功1");
			
				String req_info = params.get("req_info");
				logger.info(req_info);
			//	String resultStr = AESUtil.decryptData(req_error);
				// WXPayUtil.getLogger().error("refund:解密后的字符串:" + resultStr);
			//	Map aesMap = WXPayUtil.xmlToMap(resultStr);
				String afterDecrypt=AESUtil.decryptData(req_info);
				logger.info(afterDecrypt);
				Map aesMap = WXPayUtil.xmlToMap(afterDecrypt);
				logger.info("退款成功1");
				/** 以下为返回的加密字段: **/
				//  商户退款单号  是   String(64)  1.21775E+27 商户退款单号
				String out_refund_no = aesMap.get("out_refund_no");
				//  退款状态    是   String(16)  SUCCESS SUCCESS-退款成功、CHANGE-退款异常、REFUNDCLOSE—退款关闭
				String refund_status = aesMap.get("refund_status");
				//  商户订单号   是   String(32)  1.21775E+27 商户系统内部的订单号
				String out_trade_no = aesMap.get("out_trade_no");
				logger.info(refund_status);
				logger.info(out_trade_no);
				logger.info(out_refund_no);
			if (!WXPayConstants.SUCCESS.equals(refund_status)) {
				//这里写业务代码
				logger.info("退款成功");
				logger.info(params);
			}
		}

	}

从微信那边获得req_info后对其解密,在获得你的订单号等,来进行业务操作。

 

如果不行的话。请按下面的方法操作一下。

JAVA运行环境默认不允许256位密钥的AES加解密,解决方法就是修改策略文件

在官方网站下载JCE无限制权限策略文件

    JDK7版本JCE下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

    JDK8版本JCE下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

    下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt
    如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security目录下覆盖原来的文件
    如果安装了JDK,将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件

    实践:
    以JDK8为例,系统为WIN10,替换上述security文件夹下\policy\limited文件夹和\policy\unlimited文件夹里面的local_policy.jar和US_export_policy.jar这两个文件。

    若是在服务器上,则只有在security目录下有local_policy.jar和US_export_policy.jar,替换即可

感谢简书的推荐
链接:https://www.jianshu.com/p/6d3259e88b80
 

你可能感兴趣的:(微信支付退款结果通知(回调) AES-256-ECB解密 代码)