首先添加依赖包:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
一、解密步骤:
二、主要字段:
三、代码如下:
package com.pay;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class WXPayUtil {
private static MessageDigest sMd5MessageDigest;
private static StringBuilder sStringBuilder;
public static String getRefundDecrypt(String reqInfoSecret, String key) {
String result = "";
try {
Security.addProvider(new BouncyCastleProvider());
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] bt = decoder.decodeBuffer(reqInfoSecret);
String b = new String(bt);
String md5key = md5(key).toLowerCase();
System.out.println(md5key);
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();
}
return result;
}
public static void main(String[] args) {
String A = "cbRfjwOKrLQCOjAU0v1Ac6cDJQhK6349ngVplZHLe010obV59BJ75z05eUzG+Um3Zk9WxNO6jtk60bBDkMLmf4dZk41wSbyx/GiNNcRp3g0GdUMjGjhlyJgMAKiLbmVwcPEQnOoikcCbwGd0VmOdWcTk67kDlE4ssYW6pgBXp5bSBVotuar2Wxi0z20HGgsz7dcIVELP9+JfGwuiVV9xkhO8sPbEO4SIO2qhkRQ0QzbDYgn9gU1Iprzv6wGxFh+Bm/lJWuiBGwhFCT2fq1xEF1nDPEyW3LOWq4daCegXpvTXoXLZp9Xp6zcdxiDCsLgj8yj+q7ZNmVNt1vTUR94ZQAI1UAtCqK+1dI89DZdbifU7o0fVm/9WQqjcOIo4WemUJU7WfCsyHXBVvx8lZezoQc5ZGZOqTEZJCVKV6OF5iqvlNnZJ1byPg7BfHZnmbIjdETFMIOkq3oPicshnNlVZ9g08DvesDZzG/KJzI8NBMFfXNMjSJyuemsZ/0jFJRtKDOWoCQetWox+mORa5BPrMwvibTLkPZL0okvnyrJgFwGtE4BFTdnN/+cfuUISXMTbeZv6UjRJwD9y8B+wi4wTSUC1QXjlZEkV+RgBNnJD/n13NWUK1nlrwev/RHLsgjoMSYnF0mYec8g1BCyW0POcp4iEDaRVMrjQnACyyeMncSxA+KawdoFEOqMuAITv3B0Q9iZOTpI7yqnpquWiphXMuKovJgLp4vnPqerMMthTeaQ/rfVVO/U6Z4K/heTZKT2Y0y7kQP2GS+r/N5Qvu+J0sihJ8opoZ0AJ7ktwGrzcmkIq/DCCtAfgdAT8x3rtSXA4f8pb7WqFssdGRqgzMZR1jlcz3LD5+Du+BWK5QmyCeynywd9b6s7oYdyU0aI75LrT5EBOtEHAMGi2rmwb1X2wlEd3g4gCr2l1EEA4dUTdhsqynG0w64VHWJkvJgTOdmzMPWYksLu463qcy6dN44WuNBG0aw7uEQQIUXeRhDEwCBQVmzXNLzwQ6wHv6O9j7yvkvZll3SuGw7bbsfdSdAfQkJkLCj/MHuEKUauiV0mQS/2M=";
String key = "02a86cb980b1237eaa77fb86adf64657";
System.out.println(A);
System.out.println(key);
String B = getRefundDecrypt(A, key);
System.out.println(B);
}
public static String md5(String s) {
sMd5MessageDigest.reset();
sMd5MessageDigest.update(s.getBytes());
byte[] digest = sMd5MessageDigest.digest();
sStringBuilder.setLength(0);
for (int i = 0; i < digest.length; ++i) {
int b = digest[i] & 255;
if (b < 16) {
sStringBuilder.append(‘0‘);
}
sStringBuilder.append(Integer.toHexString(b));
}
return sStringBuilder.toString().toUpperCase();
}
static {
try {
sMd5MessageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException var1) {
}
sStringBuilder = new StringBuilder();
}
}
运行结果为:
<root>
<out_refund_no><![CDATA[R-wxgzh_test_121101940]]></out_refund_no>
<out_trade_no><![CDATA[wxgzh_test_121101940]]></out_trade_no>
<refund_account><![CDATA[REFUND_SOURCE_UNSETTLED_FUNDS]]></refund_account>
<refund_fee><![CDATA[1]]></refund_fee>
<refund_id><![CDATA[50000405672018012903361917236]]></refund_id>
<refund_recv_accout><![CDATA[支付用户零钱]]></refund_recv_accout>
<refund_request_source><![CDATA[API]]></refund_request_source>
<refund_status><![CDATA[SUCCESS]]></refund_status>
<settlement_refund_fee><![CDATA[1]]></settlement_refund_fee>
<settlement_total_fee><![CDATA[1]]></settlement_total_fee>
<success_time><![CDATA[2018-01-29 15:27:16]]></success_time>
<total_fee><![CDATA[1]]></total_fee>
<transaction_id><![CDATA[4200000098201801292992735916]]></transaction_id>
</root>