生成的head:
Authorization:WECHATPAY2-SHA256-RSA2048 mchid="171***12",nonce_str="9d2640bee3dc439e877cbe0181e0b569",signature="jFKNe+YM/UmNkwRpXnodrJuqbHZl74DKPhUgkRuowSitPH6k91SVkWP4YeQ8PP9kfzgzLFlbOn8K0XQ2AQIqzsvGVDAvneTuqg/oAuXdUSFyvNLzRlyftoqILFx0lwSEhWWST62RUS7OK+y8CXdJ7OdnKsol66UgrvKEArZ+u9l/Nk7sz5ti1cmpMYiEHsr55Iri7yw3JnA8esy1CM2eHmG3XaYduVDHQMeHwALJb+gVyNK6ZjT/6dKgY08YYMnlooKbK22qfQ4w2pJw5aI+LfTkLDjyHUI7jxL2Z4HD/CuYgK/rWEE3s6hO3s4oW2k9np8cweSkdWqdH9P9Gq8SFg==",timestamp="1751437716",serial_no="5D1E3AA427034E38F36A7D75B81F4EDE420E65B9";Wechatpay-Serial:5D1E3AA***5B9;Accept:application/json;User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;);Content-Type:application/json;
调试过过程中,httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
返回 :微信支付 商家转帐 请确认待处理的消息是否为加密后的密文
responseText = {"code":"PARAM_ERROR","message":"请确认待处理的消息是否为加密后的密文"}
请问哪里需要 加密。aasp.net4.5 使用什么方法 进行加密?
代码参考:https://developers.weixin.qq.com/community/develop/article/doc/00082cf0934a106b420a678d45b013
{
string requestBody ="
{\"appid\":\"wx48e923d9886gh\",\"out_bill_no\":\"preub15\",\"transfer_scene_id\":\"1000\",\"transfer_remark\":\"0\",\"openid\":\"oyQFh*******jBsdiwys6jt6tu4\",\"transfer_amount\":\"120\",\"total_num\":\"1\",\"user_name\":\"**堂\",\"user_recv_perception\":\"现金奖励\",\"transfer_scene_report_infos\":[{\"info_type\":\"佣金报酬\",\"info_content\":\"佣金提现报酬\"}]}";
string physicalApplicationPath = HttpContext.Current.Request.PhysicalApplicationPath;
string pemPath = physicalApplicationPath + "config/apiclient_key.pem";
var pemContent = File.ReadAllText(pemPath)
.Replace("-----BEGIN PRIVATE KEY-----", "")
.Replace("-----END PRIVATE KEY-----", "")
.Replace("\n", "");
string method = "POST";
string timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString();
string nonce = Guid.NewGuid().ToString("N");
string url = "/v3/fund-app/mch-transfer/transfer-bills";
string message = $"{method}\n{url}\n{timestamp}\n{nonce}\n{requestBody}\n";
string signature = Sign( message, pemContent);
string CertSerialNo = "5D1E3AA**********81F0E65B9";
string token = $"WECHATPAY2-SHA256-RSA2048 " +
$"mchid=\"{this.mchid}\"," +
$"nonce_str=\"{nonce}\"," +
$"signature=\"{signature}\"," +
$"timestamp=\"{timestamp}\"," +
$"serial_no=\"{CertSerialNo}\"";
string Gateway = "https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills";
// 3. 发送请求
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Gateway);
httpRequest.Method = "POST";
httpRequest.Headers.Add("Authorization", token);
httpRequest.Headers.Add("Wechatpay-Serial", CertSerialNo);
httpRequest.Accept = "application/json";
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
httpRequest.ContentType = "application/json";
HttpWebResponse httpWebResponse = null;
// 写入请求体
Stream stream = null;
byte[] data = Encoding.UTF8.GetBytes(requestBody);
stream = httpRequest.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
// 4. 获取响应
httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
string text = streamReader.ReadToEnd().Trim();
streamReader.Close();
}
protected string Sign(string message, string privateKey)
{
// SHA256withRSA
//根据需要加签时的哈希算法转化成对应的hash字符节
//byte[] bt = Encoding.GetEncoding("utf-8").GetBytes(str);
byte[] bt =Encoding.UTF8.GetBytes(message);
var sha256 = new SHA256CryptoServiceProvider();
byte[] rgbHash = sha256.ComputeHash(bt);
RSACryptoServiceProvider key = new RSACryptoServiceProvider();
var _privateKey = RSAKeyConvert.RSAPrivateKeyJava2DotNet(privateKey);
key.FromXmlString(_privateKey);
RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
formatter.SetHashAlgorithm("SHA256");//此处是你需要加签的hash算法,需要和上边你计算的hash值的算法一致,不然会报错。
byte[] inArray = formatter.CreateSignature(rgbHash);
return Convert.ToBase64String(inArray);
}
protected string RSAPrivateKeyJava2DotNet(string privateKey)
{
RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
}

字段有误,请参考加密文档https://pay.weixin.qq.com/doc/v3/merchant/4013053257
Wechatpay-Serial 对应的应该是微信支付平台证书序列号,https://pay.weixin.qq.com/doc/v3/merchant/4013053257中指明 的是使用微信支付公钥加密,需要在http请求增加一个Wechatpay-Serial请求头,并传入微信支付公钥ID"Wechatpay-Serial: PUB_KEY_ID_0116571234562024052000123400000000"(此处的公钥ID只是一个示例,每个商户号的公钥ID不一样,需传入商户号对应的公钥ID),两者如何理解 ?
1、微信平台证书(用工具下载 或接口下载两种方式获取)-- ---可以获取到 【微信平台证书序列号】和和密文ciphertext,解密后可以得到平台证书内容。
平台证书文件名类似于wechatpay_123456777B4A9CC78902B44B65E04B9751CE12.pem
2、微信支付公钥,替代1的微信支付平台证书,商户后台申请下载。
公钥id类似:PUB KEY ID 0117000800002025045698789500001779
(二选一用于回调和响应验签)
看这里https://pay.weixin.qq.com/doc/v3/merchant/4013053257