/// <summary> /// 生成请求时的签名 /// </summary> /// <param name="sPara">请求给支付宝的参数数组</param> /// <param name="_config">配置信息</param> /// <returns>签名结果</returns> private static string BuildRequestMysign(Dictionary <string, string> sParaTemp, Config _config) { //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 string prestr = UrlHelper.CreateLinkString(sParaTemp); //把最终的字符串签名,获得签名结果 string mysign = ""; switch (_config.Sign_type.ToUpper()) { case "MD5": mysign = Sign.MD5(prestr, _config.Key, _config.Input_charset); break; case "RSA": mysign = RSAFromPkcs8.sign(prestr, _config.Private_key, _config.Input_charset); break; case "0001": mysign = RSAFromPkcs8.sign(prestr, _config.Private_key, _config.Input_charset); break; default: mysign = ""; break; } return(mysign); }
/// <summary> /// 解密 /// </summary> /// <param name="inputPara">要解密数据</param> /// <returns>解密后结果</returns> public Dictionary <string, string> Decrypt(Dictionary <string, string> inputPara, Config config) { try { inputPara["notify_data"] = RSAFromPkcs8.decryptData(inputPara["notify_data"], config.Private_key, config.Input_charset); } catch (Exception e) { } return(inputPara); }
/// <summary> /// 获取返回时的签名验证结果 /// </summary> /// <param name="inputPara">通知返回参数数组</param> /// <param name="sign">对比的签名结果</param> /// <param name="_config">配置信息</param> /// <param name="isSort">是否对待签名数组排序</param> /// <returns>签名验证结果</returns> private bool GetSignVeryfy(Dictionary <string, string> inputPara, string sign, Config _config, bool isSort = true) { Dictionary <string, string> sPara = new Dictionary <string, string>(); //Core.Log.Debug("sign_type=" + inputPara["sign_type"]); //过滤空值、sign与sign_type参数 sPara = UrlHelper.FilterPara(inputPara, "sign", "sign_type"); if (!isSort) { sPara = SortNotifyPara(sPara); //固定顺序排序 } //获取待签名字符串 string preSignStr = UrlHelper.CreateLinkString(sPara); // Core.Log.Debug("Public_key=" + _config.Public_key+ "\r\n 待签名字符串=" + preSignStr+ "\r\n,签名应该结果:"+sign); //获得签名验证结果 bool isSgin = false; if (sign != null && sign != "") { switch (_config.Sign_type) { case "MD5": isSgin = Sign.VerifyMD5(preSignStr, sign, _config.Key, _config.Input_charset); break; case "RSA": isSgin = RSAFromPkcs8.verify(preSignStr, sign, _config.Public_key, _config.Input_charset); break; case "0001": isSgin = RSAFromPkcs8.verify(preSignStr, sign, _config.Public_key, _config.Input_charset); break; default: break; } } return(isSgin); }
/// <summary> /// 解析远程模拟提交后返回的信息 /// </summary> /// <param name="strText">要解析的字符串</param> /// <param name="_config">配置信息</param> /// <returns>解析结果</returns> public static Dictionary <string, string> ParseResponse(string strText, Config _config) { //以“&”字符切割字符串 string[] strSplitText = strText.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); //放入字典类数组中 strValue = HttpUtility.UrlDecode(strValue); dicText.Add(strKey, strValue); } if (dicText.ContainsKey("res_data")) { //解析加密部分字符串(RSA与MD5区别仅此一句) if (_config.Sign_type == "0001") { dicText["res_data"] = RSAFromPkcs8.decryptData(dicText["res_data"], _config.Private_key, _config.Input_charset); } //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) { dicText.Add("res_error", exp.ToString()); } } else if (dicText.ContainsKey("res_error")) { //token从res_data中解析出来(也就是说res_data中已经包含token的内容) XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(dicText["res_error"]); string strRequest_token = xmlDoc.SelectSingleNode("/err/detail").InnerText; dicText["res_error"] = strRequest_token; //dicText.Add("res_error", strRequest_token); } catch (Exception exp) { dicText.Add("res_error", exp.ToString()); } } return(dicText); }