/// <summary> /// 获取签名字符串 /// </summary> /// <param name="dic">待签名参数</param> /// <param name="key">签名密钥</param> /// <param name="signType">签名加密方法</param> /// <returns></returns> public static string GetSign(SortedDictionary <string, object> dic, string key, SignType signType) { var strSign = string.Empty; var strRequestData = string.Empty; StringBuilder sbRequest = new StringBuilder(); foreach (var item in dic) { if (item.Key.ToLower() != "sign" && item.Key.ToLower() != "sign_type" && item.Value != "" && item.Value != null) { sbRequest.AppendFormat("{0}={1}&", item.Key, item.Value); } } if (sbRequest.Length > 0) { if (!string.IsNullOrEmpty(key)) { strRequestData = string.Format("{0}&key={1}", sbRequest.ToString(), key.Trim()); } else { strRequestData = sbRequest.ToString().TrimEnd('&'); } } switch (signType) { case SignType.MD5: { strSign = MD5CryptionUtil.Sign(strRequestData, "").ToUpper(); break; }; case SignType.SHA1: { strSign = SecurityUtil.SHA1Encrypt(strRequestData); break; }; case SignType.SHA256: { strSign = SecurityUtil.SHA256Encrypt(strRequestData); break; } default: break; } return(strSign); }
/// <summary> /// 获取MD5签名 /// </summary> /// <param name="key">签名密钥</param> /// <param name="str">签名字符串</param> /// <returns>MD5签名</returns> public static string GetMD5Sign(string key, string str) { str += "&key=" + key; return(MD5CryptionUtil.Sign(str).ToUpper()); }