微信退款结果通知

import java.io.BufferedReader;
import java.security.Security;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**微信退款结果通知 解密步骤如下:
*(1)对加密串A做base64解码,得到加密串B
*(2)对商户key做md5,得到32位小写key* ( key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 )
*(3)用key*对加密串B做AES-256-ECB解密(PKCS7Padding)
**/
public class AppOrder {
    public static String WECHAT_SMG_APP_ID = "公众账号ID";
    public static String WECHAT_SMG_SECRET_KEY = "商户key";
    public static String WECHAT_SMG_MCH_ID = "商户号";
    public static void main(String[] args) throws Exception {
		String xml="\r\n" + 
				"    SUCCESS\r\n" + 
				"    \r\n" + 
				"        \r\n" + 
				"    \r\n" + 
				"    \r\n" + 
				"        \r\n" + 
				"    \r\n" + 
				"    \r\n" + 
				"        \r\n" + 
				"    \r\n" + 
				"    \r\n" + 
				"        \r\n" + 
				"    \r\n" + 
				"";
		Map notify = WXPayUtil.xmlToMap(xml);
		String reqInfo = notify.get("req_info").trim();
		
        String result = "";
        try {
			Security.addProvider(new BouncyCastleProvider());
			byte[] bt = Base64.decodeBase64(reqInfo);
			String md5key = DigestUtils.md5Hex(WECHAT_SMG_SECRET_KEY).toLowerCase();
			SecretKey secretKey = new SecretKeySpec(md5key.getBytes(), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			byte[] resultbt = cipher.doFinal(bt);
			result = new String(resultbt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
	}
}

	org.bouncycastle
	bcprov-jdk15on
	1.60

        

 

你可能感兴趣的:(微信退款结果通知)