/// <summary> /// 构成URL参数的sign值 /// </summary> /// <param name="dic">参数的字典集合</param> /// <param name="key">安全校验码</param> /// <returns>返回sign的值</returns> /// <remarks>参与加密的参数存入Dictionary,参数的值不允许为空,若该参数为空,不要把这个参数加到Dictionary集合里面</remarks> public static string GetSign(Dictionary <string, string> dic, string key) { //构造数组 string[] Oristr = new string[dic.Count]; int ct = 0; foreach (KeyValuePair <string, string> entry in dic) { if (!String.IsNullOrEmpty(entry.Value)) { Oristr[ct] = entry.Key + "=" + entry.Value; ct++; } } //进行排序 string[] Sortedstr = Alipay.BubbleSort(Oristr); //构造待md5摘要字符串 System.Text.StringBuilder prestr = new System.Text.StringBuilder(); for (int i = 0; i < Sortedstr.Length; i++) { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i]); } else { prestr.Append(Sortedstr[i] + "&"); } } prestr.Append(key);//追加key //生成Md5摘要 return(Alipay.GetMD5(prestr.ToString(), _Input_Charset)); }
/// <summary> /// 构造md5签名字符串 /// </summary> /// <param name="nvc">NameValues集合</param> /// <param name="key">安全校验码</param> /// <returns>返回排序后的字符串(自动剔除末尾的sign和sign_type类型)</returns> private string BuildSignString(System.Collections.Specialized.NameValueCollection nvc, string key) { string[] Sortedstr = Alipay.BubbleSort(nvc.AllKeys); //对参数进行排序 StringBuilder prestr = new StringBuilder(); //构造待md5摘要字符串 for (int i = 0; i < Sortedstr.Length; i++) { if (nvc.Get(Sortedstr[i]) != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type") { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i] + "=" + nvc.Get(Sortedstr[i])); } else { prestr.Append(Sortedstr[i] + "=" + nvc.Get(Sortedstr[i]) + "&"); } } } prestr.Append(key);//追加key return(prestr.ToString()); }