微信退款异步通知报文解密

编程

拿到报文 对req_info 用商户秘钥进行解密 

package com.ly.upg.message.util;

import com.alibaba.fastjson.JSONObject;

import com.snake.bsys.common.log.Log;

import com.tencent.common.MD5;

import com.tencent.common.XMLParser;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.spec.SecretKeySpec;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.NoSuchProviderException;

import java.security.Security;

import java.util.Base64;

import java.util.HashMap;

import java.util.Map;

/**

* @Title:

* @Description:微信退款报文解密

* @Created by yangjie on 2020/1/4/15:18.

*/

public class WxDecryptUtil {

public static Map excute(String keyStr,String orgStr) throws BadPaddingException, IllegalBlockSizeException {

//MD5.MD5Encode(secretKey).toLowerCase().getBytes()

SecretKeySpec key = new SecretKeySpec((MD5.MD5Encode(keyStr).toLowerCase().getBytes()), "AES");

Security.addProvider(new BouncyCastleProvider());

Cipher cipher = null;

try {

cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (NoSuchProviderException e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

}

try {

cipher.init(Cipher.DECRYPT_MODE, key);

} catch (InvalidKeyException e) {

e.printStackTrace();

}

Base64.Decoder decoder = Base64.getDecoder();

String reqInfo = orgStr.replaceAll(" ","+");

byte[] decryptContent = cipher.doFinal(decode(reqInfo));

try {

Map map = new HashMap();

map = XMLParser.getMapFromXML(new String(decryptContent, "utf-8"));

Log.logger.info("微信解密后的报文-->"+ JSONObject.toJSONString(map));

return map;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static byte[] decode(String encodedText){

final Base64.Decoder decoder = Base64.getDecoder();

return decoder.decode(encodedText);

}

public static void main(String[] args) {

try {

// req_info

String org = "Nr2R4wa3OQpKFFV3Em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yhvClKXiqUO2uR5kzg84Pyb";

Map test = excute(key,org);

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

}

}

}

package com.ly.upg.message.util;

import com.alibaba.fastjson.JSONObject;

import com.snake.bsys.common.log.Log;

import com.tencent.common.MD5;

import com.tencent.common.XMLParser;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import org.xml.sax.SAXException;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import javax.xml.parsers.ParserConfigurationException;

import java.io.IOException;

import java.security.Security;

import java.util.Base64;

import java.util.HashMap;

import java.util.Map;

/**

* @Title:

* @Description:

* @Created by yangjie on 2020/1/3/18:46.

*/

public class CipherTextUtil {

/**

* 密钥算法

*/

private static final String ALGORITHM = "AES";

/**

* 加解密算法/工作模式/填充方式

*/

private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";

static {

try {

Security.addProvider(new BouncyCastleProvider());

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* AES解密

*/

public static String decryptData(byte[] b,SecretKeySpec key) throws Exception {

Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);

cipher.init(Cipher.DECRYPT_MODE, key);

return new String(cipher.doFinal(b));

}

public static Map decrypt(String secretKey, String orgStr) throws IOException, SAXException, ParserConfigurationException {

SecretKeySpec key = new SecretKeySpec(MD5.MD5Encode(secretKey).toLowerCase().getBytes(), ALGORITHM);

Log.logger.info("微信退款解密原始字符串-->"+orgStr);

Log.logger.info("微信退款解密key-->"+secretKey);

byte[] b = new byte[0];

try {

b = Base64Util.decode(orgStr.replaceAll(" ","+"));

if(b == null|| b.length == 0){

Log.logger.error("微信解密原始字符串-解码失败decode.");

return null;

}

}catch (Exception e){

Log.logger.error("微信解密原始字符串-解码异常decode.");

}

String B = null;

try {

Log.logger.info("微信退款报文解密AES解密开始:解码b="+JSONObject.toJSONString(b)+",秘钥key="+key.toString());

Log.logger.info("微信退款报文解密AES解密开始:解码b="+b.length);

B = decryptData(b,key);

} catch (Exception e) {

Log.logger.error("微信退款报文解密AES解密失败"+e.getMessage());

e.printStackTrace();

return null;

}

Map map = new HashMap();

map = XMLParser.getMapFromXML(B);

Log.logger.info("微信解密后的报文-->"+JSONObject.toJSONString(map));

return map;

}

public static byte[] decode(String encodedText){

final Base64.Decoder decoder = Base64.getDecoder();

return decoder.decode(encodedText);

}

public static void main(String[] args) throws Exception {

String A = "Nr2R4wa3OQpKFFV3EmMB463MPGliaPI78OKgLy61tSwbDwE2SCPgUYG9yls54PSOMmgPdCUia2BYKAnzFdNmQdyWDNIN4XO9mHNcFw/ubwVyeBqzs1CVvqPZEhV4hEfgGrK81x58YsvewULHS0JAkZgtGXDFgduaaV179Wny7YiogFk0i/hromZgGyG1+emNJxJhzo7KkEqSSztRMTOJp+28RyV4iEIWD1WvKJkiKx4/yNOXVsRTaKXEeKnlMj5v01lVdsxUa1HziW5PT6l7wp0bfOVU3mc2+aYf7dgqT9NWEZ56bU92+27l/GKa3fFTDvjFNNqDcGP66AmfAToL8D09peXCj8t2J9Af7+5mngRqGVzNM/FBOnK6th3nmDZFp9Acoy4wxskDloLUB4KBZjtnZYyEAq9Bex7AZMwsW10uLy8TXLwUh9j0WGXORKytne2gbnHEHfbGp/6xmp1EOZKHRJPAW198cYrzEy8WqTVjv3fqWqnw2rrcDwFp57PLk4e1QPec4rRwloP71p4oS8q6Hj3ZLYENRI74iofhUhZRryPtcQNeaXDBWC0Q2VWW/va6BpaKA1f0eOG5ITcoaoqaECFTz10B9vyX5MbVIjjSRmiQV2bY3d4c2y794Bh3VxZkLrEJ3pjbb9Jc8abwfDD9gjxlGW/gj9AbnKpjuoeGsj/RJqzspmNmwQxnxkQ7MEejbX0xgc0y4mq6EuFxJcqkEcFxNwrjheIqTNkGbIk8vjeimbKIh820GKmKmF4HNYV0z6h3dCF/7Jp9HhnxpvQdIwDpUzeQe8QXI0+I3yO36hdXeo3qH/bUtbQO/nN20HwbDvVanMIvA5Gp1RcRngY2uFx38CReqmIhbn5j4L+SwtmQ6B0LXqlAb/HQPeXd891Bq6vf8GwdQGygZOIETzddfblhCPBvNe0a7oaWY7ygXNM+HQw/9T1tEfesrBy2JL4BPhAjqlTUDZfldLd3Mf0MhMJNaEWXI+iUFRsXbRlIHuURglFzrZWvl/UzPGZk/nhpp5p4yX/HF9z0iObanrxLIJRAdPB1qAReLoC7BztScICQ8vF5euSk3cczPeU8";

byte[] b = Base64Util.decode(A.replaceAll(" ","+"));

Map map = decrypt("HqPzKWBaZyhvClKXiqUO2uR5kzg84Pyb", A);

System.out.println(JSONObject.toJSONString(map));

}

}

很诡异的问题是在服务器 总是报 pad block corrupted 错误 至今未解决 难搞哦

以上是 微信退款异步通知报文解密 的全部内容, 来源链接: utcz.com/z/512441.html

回到顶部