/// <summary> /// 验签(排序验签) /// </summary> /// <param name="pSArrary">待签名数组</param> /// <param name="pSignedString">签名(支付宝返回sign)</param> /// <param name="pPublickey">支付宝公钥</param> /// <param name="pInputCharset">编码格式</param> /// <returns>返回验签结果,true(相同),false(不相同)</returns> public static bool Verify(SortedDictionary <string, string> pSArrary, string pSignedString, string pPublickey, string pInputCharset) { Dictionary <string, string> sPara = FilterPara(pSArrary); string content = CreateLinkString(sPara); bool b = RSAFromPkcs8.Verify(content, pSignedString, pPublickey, pInputCharset); return(b); }
/// <summary> /// 签名 /// </summary> /// <param name="dicArrayPre">待签名字符串</param> /// <param name="privatekey">商户私钥</param> /// <param name="input_charset">编码格式</param> /// <returns>经过UrlEncode转码后的签名字符串</returns> public static string BuildMysign(SortedDictionary <string, string> dicArrayPre, string privatekey, string input_charset) { Dictionary <string, string> dicArray = FilterPara(dicArrayPre); string prestr = CreateLinkString(dicArray); string mysign = RSAFromPkcs8.Sign(prestr, privatekey, input_charset); mysign = HttpUtility.UrlEncode(mysign, Encoding.GetEncoding(input_charset)); //此处需要对签名进行Encode,否则出现+号等特殊字符,通过base64转换并post提交给支付宝服务器会丢失,变成空格 return(mysign); }
/// <summary> /// 解析远程模拟提交后返回的信息 /// </summary> /// <param name="pStrText">要解析的字符串</param> /// <param name="pSignType">加密方式</param> /// <param name="pPrivateKey">RSA加密算法的私有密钥</param> /// <param name="InputCharset">编码方式</param> /// <returns>解析结果</returns> public static Dictionary <string, string> ParseResponse(string pStrText, string pSignType, string pPrivateKey, string InputCharset) { //以“&”字符切割字符串 string[] strSplitText = pStrText.Split('&'); //把切割后的字符串数组变成变量与数值组合的字典数组 Dictionary <string, string> dicText = new Dictionary <string, string>(); for (int i = 0; i < strSplitText.Length; i++) { //获得第一个=字符的位置 int nPos = strSplitText[i].IndexOf('='); //获得字符串长度 int nLen = strSplitText[i].Length; //获得变量名 string strKey = strSplitText[i].Substring(0, nPos); //获得数值 string strValue = strSplitText[i].Substring(nPos + 1, nLen - nPos - 1); //放入字典类数组中 dicText.Add(strKey, strValue); } if (dicText["res_data"] != null) { //解析加密部分字符串(RSA与MD5区别仅此一句) if (pSignType == "0001") { dicText["res_data"] = RSAFromPkcs8.DecryptData(dicText["res_data"], pPrivateKey, InputCharset); } //token从res_data中解析出来(也就是说res_data中已经包含token的内容) XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(dicText["res_data"]); string strRequest_token = xmlDoc.SelectSingleNode("/direct_trade_create_res/request_token").InnerText; dicText.Add("request_token", strRequest_token); } catch (Exception exp) { throw exp; } } return(dicText); }
/// <summary> /// 解密 /// </summary> /// <param name="pContent">待解密字符串</param> /// <param name="pPrivateKey">商户私钥</param> /// <param name="pInputCharset">编码格式</param> /// <returns>返回明文</returns> public static string Decrypt(string pContent, string pPrivateKey, string pInputCharset) { string strDecryptData = RSAFromPkcs8.DecryptData(pContent, pPrivateKey, pInputCharset); return(strDecryptData); }
/// <summary> /// 验签(不排序 Notify验签用这个) /// </summary> /// <param name="pContent">待验签字符串</param> /// <param name="pSignedString">签名(支付宝返回sign)</param> /// <param name="pPublickey">支付宝公钥</param> /// <param name="pInputCharset">编码格式</param> /// <returns>返回验签结果,true(相同),false(不相同)</returns> public static bool Verify(string pContent, string pSignedString, string pPublickey, string pInputCharset) { bool b = RSAFromPkcs8.Verify(pContent, pSignedString, pPublickey, pInputCharset); return(b); }