private bool GetSignVeryfy(SortedDictionary <string, string> inputPara, string sign) { if (!string.IsNullOrEmpty(sign)) { switch (SignType) { case "MD5": return(AlipayMD5.Verify(CreateLinkString(FilterPara(inputPara)), sign, PartnerKey, Charset)); case "RSA": return(AlipayRSA.Verify(CreateLinkString(FilterPara(inputPara)), sign, PublicKey, Charset)); } } return(false); }
/// <summary>创建当前配置的签名 /// </summary> private string BuildRequestMysign() { string mysign = ""; if (_config.SignType == "RSA") { mysign = RSAFromPkcs8.sign("", _config.PrivateKey, _config.InputCharset); } if (_config.SignType == "MD5") { mysign = AlipayMD5.Sign("", _config.Key, _config.InputCharset); } return(mysign); }
/// <summary> /// 获取返回时的签名验证结果 /// </summary> /// <param name="inputPara">通知返回参数数组</param> /// <param name="sign">对比的签名结果</param> /// <param name="isSort">是否对待签名数组排序</param> /// <returns>签名验证结果</returns> private bool GetSignVeryfy(Dictionary <string, string> inputPara, string sign, bool isSort) { Dictionary <string, string> sPara = new Dictionary <string, string>(); //过滤空值、sign与sign_type参数 sPara = Core.FilterPara(inputPara); if (isSort) { //根据字母a到z的顺序把参数排序 sPara = Core.SortPara(sPara); } else { sPara = SortNotifyPara(sPara); } //获取待签名字符串 string preSignStr = Core.CreateLinkString(sPara); //获得签名验证结果 bool isSgin = false; if (sign != null && sign != "") { switch (AlipayMobileConfig.Sign_type) { case "MD5": isSgin = AlipayMD5.Verify(preSignStr, sign, AlipayMobileConfig.Key, AlipayMobileConfig.Input_charset); break; case "RSA": isSgin = RSAFromPkcs8.verify(preSignStr, sign, AlipayMobileConfig.Public_key, AlipayMobileConfig.Input_charset); break; case "0001": isSgin = RSAFromPkcs8.verify(preSignStr, sign, AlipayMobileConfig.Public_key, AlipayMobileConfig.Input_charset); break; default: break; } } return(isSgin); }
}/// <summary> /// 验证签名 /// </summary> /// <param name="dic"></param> /// <param name="key"></param> /// <returns></returns> private bool CheckSign(Dictionary <string, string> dic, string key) { dic = SortDictionary(dic); string sigSource = ""; foreach (var item in dic) { if (item.Value != null && item.Value.Trim().Length > 0 && item.Key != "sign_type" && item.Key != "sign") { sigSource = sigSource + item.Key + "=" + HttpUtility.UrlDecode(item.Value) + "&"; } } if (sigSource.Length > 1) { sigSource = sigSource.Substring(0, sigSource.Length - 1); } sigSource += "&key=" + key; return(AlipayMD5.getmd5(sigSource).ToUpper() == dic["sign"]); }
public void CreateOrder() { string notifyUrl = ConfigurationManager.AppSettings["notifyurl"].ToString(); //回 调地址 string mchid = ConfigurationManager.AppSettings["merno"].ToString(); //商户号 string key = ConfigurationManager.AppSettings["key"].ToString(); //商户密钥 string srcCode = ConfigurationManager.AppSettings["srcCode"].ToString(); //商户唯一标识 Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("src_code", srcCode); dic.Add("mchid", mchid); dic.Add("out_trade_no", DateTime.Now.ToString("yyyyMMddhhmmss")); dic.Add("total_fee", "100"); dic.Add("time_start", DateTime.Now.ToString("yyyyMMddhhmmss")); dic.Add("goods_name", "充值"); dic.Add("trade_type", "60104"); dic.Add("finish_url", notifyUrl); string tempstr = ConvertDic2Urlparam(dic) + "&key=" + key; //转换为URLPARAM串 string sign = AlipayMD5.getmd5(tempstr).ToUpper(); //MD5加密 dic.Add("sign", sign); string url = ConfigurationManager.AppSettings["tradeurl"].ToString() + "/trade/pay"; //API地址 string pdata = ConvertDic2Urlparam(dic); //转换为URLPARAM串 string r = PostRequest(url, pdata, Encoding.UTF8, Encoding.UTF8); interfaceReturnDataModel rdic = Newtonsoft.Json.JsonConvert.DeserializeObject <interfaceReturnDataModel>(r); if (rdic.respcd == "0000" && CheckSign(Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(Newtonsoft.Json.JsonConvert.SerializeObject(rdic.data)), key)) { //成功 } else { //失败 } //db.ExecSql("insert tb_paydata (payid,paydata,datatype) values(" + stringConstrunctor.GetQuotedStr(orderid) + "," + stringConstrunctor.GetQuotedStr(Newtonsoft.Json.JsonConvert.SerializeObject(rd)) + ",1)"); }
/// <summary> /// 生成请求时的签名 /// </summary> /// <param name="sPara">请求给支付宝的参数数组</param> /// <returns>签名结果</returns> private static string BuildRequestMysign(Dictionary <string, string> sPara) { //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 string prestr = Core.CreateLinkString(sPara); //把最终的字符串签名,获得签名结果 string mysign = ""; switch (_sign_type) { case "MD5": mysign = AlipayMD5.Sign(prestr, _key, _input_charset); break; default: mysign = ""; break; } return(mysign); }
/// <summary> /// 获取Sign /// </summary> protected override void SetSign() { var sortTempdic = new SortedDictionary <string, string>(Paras); var tempdic = AliPayFunction.FilterPara(sortTempdic); var prestr = AliPayFunction.CreateLinkString(tempdic); switch (SecID) { case "MD5": Sign = AlipayMD5.Sign(prestr, Channel.MD5Key, InputCharset); break; case "RSA": Sign = RSAFromPkcs8.Sign(prestr, Channel.RSA_PrivateKey, InputCharset); break; case "0001": Sign = RSAFromPkcs8.Sign(prestr, Channel.RSA_PrivateKey, InputCharset); break; default: throw new Exception("未知的加密方式:" + SecID); } }
public int RunService(string TradeType, string TradeMsg, ref string TradeMsgOut) { int i = 0; string guid = Guid.NewGuid().ToString("N").ToUpper(); //log.Info("[" + guid + "]"); log.InfoFormat("[{2}][{0}].IN {1}", TradeType, LogUnity.I.ShowXml(TradeMsg), guid); //string outxml = ""; IBaseMessage objtype = null; try { objtype = (IBaseMessage)ToolUnity.LoadAssembly(TradeType); } catch (Exception ex) { //outxml = ToolUnity.ServiceERR(TradeType, ex); TradeMsgOut = ToolUnity.ServiceERR(TradeType, ex); i = -1; goto last; } #region 验证数字签名 //为适应老版本,这个参数设置成0,以后新上线的 都必须设置成1 string Signature = ConfigurationManager.AppSettings["Signature"]; if (Signature == "1") { XmlDocument doc = new XmlDocument(); doc.LoadXml(TradeMsg); string key = ""; string mac = ""; string sign = ""; if (doc.DocumentElement != null) { string head = doc.DocumentElement.Name; XmlNode nodemac = doc.SelectSingleNode(head + "/BASEINFO/MACDZ"); if (nodemac == null) { TradeMsgOut = ToolUnity.ServiceERR(TradeType, "MACDZ未分配!"); return(-1); } mac = nodemac.InnerText; XmlNode nodekey = doc.SelectSingleNode(head + "/BASEINFO/KEY"); if (nodekey == null) { TradeMsgOut = ToolUnity.ServiceERR(TradeType, "校验码未分配!"); return(-1); } key = nodekey.InnerText; XmlNode nodesign = doc.SelectSingleNode(head + "/BASEINFO/SIGN"); if (nodesign == null) { TradeMsgOut = ToolUnity.ServiceERR(TradeType, "数字验证未分配!"); return(-1); } sign = nodesign.InnerText; XmlNode BASEINFO = doc.SelectSingleNode(head + "/BASEINFO"); BASEINFO.RemoveChild(nodesign); } string inxml = doc.InnerXml; DataTable dt = DBVisitor.ExecuteTable($"select * from GY_REGISTERED where mac='{mac}'"); if (dt.Rows.Count <= 0) { TradeMsgOut = ToolUnity.ServiceERR(TradeType, "未注册!"); i = -1; return(i); } if (dt.Rows[0]["KEY"].ToString() != key) { TradeMsgOut = ToolUnity.ServiceERR(TradeType, "校验码验证不通过!"); i = -1; return(i); } string mysign = AlipayMD5.Sign(inxml, dt.Rows[0]["KEY"].ToString(), "utf-8"); if (mysign != sign) { TradeMsgOut = ToolUnity.ServiceERR(TradeType, "签名验证错误!"); i = -1; return(i); } } #endregion try { objtype.MessageID = guid; objtype.ParseInXml(TradeMsg); objtype.ProcessMessage(); //outxml = objtype.ParseOutXml(); TradeMsgOut = objtype.ParseOutXml(); //LogUnity.I.Info(0, TradeType, TradeMsg, outxml, "", guid); } catch (Exception ex) { //outxml = objtype.ParseOutXml(ex, ConfigurationManager.AppSettings["enabledymc"] == "0"); TradeMsgOut = objtype.ParseOutXml(ex, ConfigurationManager.AppSettings["enabledymc"] == "0"); //LogUnity.I.Insert(-1, TradeType, TradeMsg, outxml, ex.StackTrace.Trim(), guid); //LogUnity.I.Insert(-1, TradeType, TradeMsg, TradeMsgOut, ex.StackTrace.Trim(), guid); i = -1; } last: { //log.InfoFormat("[{0}].OUT {1}", TradeType, outxml + "\r\n"); log.InfoFormat("[{2}][{0}].OUT {1}", TradeType, TradeMsgOut, guid); return(i); } }