C#解密退款req_info结果通知

微信支付退款结果通知API地址:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_16&index=10

static void Main(string[] args)
{
string reqInfo = "加密字符串";
string result= DecodeReqInfo(reqInfo);//解密后的结果

}

public static string DecodeAES256ECB(string s, string key)
{
string r = null;
try
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
byte[] toEncryptArray = Convert.FromBase64String(s);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
r = UTF8Encoding.UTF8.GetString(resultArray);
}
catch { }
return r;
}

public static string DecodeReqInfo(string s)//s需要解密的字符串
{
string r = null;
string key = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(_key, "md5").ToLower();//秘钥
r = DecodeAES256ECB(s, key);
return r;
}

参考:http://www.w3dev.cn/article/20171011/cshart-decode-weixin-pay-req-info.aspx

转载于:https://www.cnblogs.com/sunshine-zhouqin/p/10156616.html

好的,针对你的问题,我可以提供以下解密微信退款通知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` 包,需要在项目中引入相关的依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值