微信退款通知req_info解密java_微信退款通知信息解密
时间: 2024-01-12 19:04:16 浏览: 119
java实现微信退款功能
好的,针对你的问题,我可以提供以下解密微信退款通知req_info的Java代码示例:
```java
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
public class WechatRefundNotify {
private static final String PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" +
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDc8j2WJbF/1kxva8sUcU9e5n9v\n" +
"GzOzX4FfB1z8VfJmP9AKJj1ZyDpMgE1l8zZxWzO0J8m1cUH3Rj5HbH+VjwVJcWj0\n" +
"g9yYb2DvZUnK0vSjxXW0nHv+9UJrGzqHv+5j1EF8Av3d0mPv5dZ1N2r7sRwZCj3s\n" +
"eLmZs5Ql5Z+8n0qazQIDAQAB\n" +
"-----END PUBLIC KEY-----";
public static void main(String[] args) throws Exception {
String encryptedReqInfo = "Jzjy4tPnE80DZzJGV8O5JtGJ2Wgc9LjNv+KMrDR/RNjXuytIg2+Gj0btS/3kC9pUgZG0sGkkSHZr6W0X9Ql4r4Bx0+6LX2aC4HfjZjNjQD9pEnYz570O5V7LjVnX3W8LLKz4lwQhZoDUEjGgkKvQzj5MmSmHjV7F1sLqWlXzTQVbVJpKl0tDwvJuZjNYWcV9+HlJnC3GphH5q2q+P6yV6lV3pcz6j0v8Z+JyK2+D8w3yUZ9K9t0pB27+qI3W6Qn6kJnq+J8Uz6F7v4Yn8X9aZ8NQgD7SWu0PQOg9nH1mz0JW5Ur0L+J2nXK4I+oMk0hO5LX5Nx/4f+54bCzVcBh1VlJ2Z9+6Fq0g==";
String mchKey = "your_mch_key";
String reqInfo = decryptReqInfo(encryptedReqInfo, mchKey);
System.out.println(reqInfo);
}
public static String decryptReqInfo(String encryptedReqInfo, String mchKey) throws Exception {
byte[] encryptedReqInfoBytes = Base64.getDecoder().decode(encryptedReqInfo);
// 解密AES密钥
byte[] mchKeyBytes = Base64.getDecoder().decode(mchKey);
Key aesKey = new javax.crypto.spec.SecretKeySpec(mchKeyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] decryptedAesKeyBytes = cipher.doFinal(encryptedReqInfoBytes, 0, 32);
// RSA解密req_info
byte[] encryptedReqInfoNoAesKeyBytes = new byte[encryptedReqInfoBytes.length - 32];
System.arraycopy(encryptedReqInfoBytes, 32, encryptedReqInfoNoAesKeyBytes, 0, encryptedReqInfoNoAesKeyBytes.length);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
InputStream inputStream = new ByteArrayInputStream(PUBLIC_KEY.getBytes());
byte[] publicKeyBytes = new byte[inputStream.available()];
inputStream.read(publicKeyBytes);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] reqInfoBytes = cipher.doFinal(encryptedReqInfoNoAesKeyBytes);
return new String(reqInfoBytes);
}
}
```
其中,`encryptedReqInfo` 是微信退款通知接口返回的 `req_info` 参数的值,`mchKey` 是商户密钥,`PUBLIC_KEY` 是微信支付平台的公钥,需要在微信商户平台获取。
需要注意的是,上述代码中使用了 `javax.crypto` 和 `java.security` 包,需要在项目中引入相关的依赖。
阅读全文