asp.net中md5加密實現代碼

來源:互聯網
上載者:User
 代碼如下 複製代碼

public class FormatStr
{
#region MD5加密
/// <summary>
/// MD5加密操作
/// </summary>
/// <param name="str">加密的字串</param>
/// <returns></returns>
public static string MD5(string str) { 
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
#endregion 

#region 去除HTML標記
/// <summary>
/// 去除HTML標記
/// </summary>
/// <param name="NoHTML">包括HTML的源碼 </param>
/// <returns>已經去除後的文字</returns>
public static string NoHTML(string Htmlstring)
{
//刪除指令碼
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//刪除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([rn])[s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", """, RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(d+);", "", RegexOptions.IgnoreCase);

Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("rn", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

return Htmlstring;
}
#endregion 

#region 去除非法字串
/// <summary>
/// 去除非法字串
/// </summary>
/// <param name="strChar">原字串</param>
/// <returns>過濾過的字串</returns>
public static string ReplaceBadChar(string strChar)
{
if (strChar.Trim() == "")
{
return "";
}
else
{
strChar = strChar.Replace("'", "");
strChar = strChar.Replace("*", "");
strChar = strChar.Replace("?", "");
strChar = strChar.Replace("(", "");
strChar = strChar.Replace(")", "");
strChar = strChar.Replace("<", "");
strChar = strChar.Replace("=", "");
return strChar.Trim();
}
}
#endregion

#region 檢察是否都是數字
/// <summary>
/// 檢察是否都是數字
/// </summary>
/// <param name="str">要檢查的字串</param>
/// <returns>bool</returns>
public static bool IsNumeric(string str)
{
Regex reg = new Regex(@"^[+]?d*$");
return reg.IsMatch(str);
}
#endregion

#region 檢察是否正確的Email格式
/// <summary>
/// 檢察是否正確的Email格式
/// </summary>
/// <param name="str">要檢查的字串</param>
/// <returns>bool</returns>
public static bool IsEmail(string str)
{
Regex reg = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
return reg.IsMatch(str);
}
#endregion

#region 檢察是否正確的日期格式
/// <summary>
/// 檢察是否正確的日期格式
/// </summary>
/// <param name="str">要檢查的字串</param>
/// <returns>bool</returns>
public static bool IsDate(string str)
{
//考慮到了4年一度的366天,還有特殊的2月的日期
Regex reg = new Regex(@"^((((1[6-9]|[2-9]d)d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]d|3[01]))|(((1[6-9]|[2-9]d)d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]d|30))|(((1[6-9]|[2-9]d)d{2})-0?2-(0?[1-9]|1d|2[0-8]))|(((1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?d):[0-5]?d:[0-5]?d$");
return reg.IsMatch(str);
}
#endregion

#region HTML轉換為字串
/// <summary>
/// HTML轉換為字串,轉換標識
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string htmToStr(string str)
{
return str.Replace("'", "mySQ;").Replace(""", "myDQ;").Replace("<", "myLt;").Replace(">", "myGt;").Replace("-", "myMl;").Replace("_", "myBl;").Replace("%", "myBs;").Replace("?", "myQe;").Replace("*", "myAl;");
}
#endregion

#region 字串轉換為HTML
/// <summary>
/// 字串轉換為HTML,將還原標識
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string strToHtm(string str)
{
if (str == null) return "";
return str.Replace("mySQ;", "'").Replace("myDQ;", """).Replace("myLt;", "<").Replace("myGt;", ">").Replace("myMl;", "-").Replace("myBl;", "_").Replace("myBs;", "%").Replace("myQe;", "?").Replace("myAl;", "*");
}
#endregion

#region 文本轉換為HTML
/// <summary>
/// 文本轉換為HTML,更換(n)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string textToHtm(string str)
{
return str.Replace("t", " ").Replace("<", "<").Replace(">", ">").Replace(" ", " ").Replace("rn", "<br>").Replace("n", "<br>");
}
#endregion

#region 文本轉換為輸入框內容
/// <summary>
/// 文本轉換為輸入框內容,
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string HtmToInputValue(string str)
{
//return str.Replace("<","<").Replace(">",">").Replace(""",""").Replace("&","&");
return str.Replace(""", """);
}
#endregion

#region 防Sql注入攻擊
/// <summary>
/// 防Sql注入攻擊
/// </summary>
/// <param name="strQueryName"></param>
/// <returns></returns>
public static string getQueryString(string strQueryName)
{
//(')(--)(or )(=)
System.Web.HttpRequest myRequest = System.Web.HttpContext.Current.Request;
string str1 = myRequest.QueryString[strQueryName];
if (str1 == null) return "";
str1 = str1.Replace("'", "’").Replace("--", "—").Replace("=", "=").Replace("or", "o r").Replace("Or", "O r").Replace("OR", "O R").Replace("oR", "o R").Replace(">", "〉").Replace("<", "〈");
return str1;
}
#endregion

#region 防Sql注入攻擊
/// <summary>
/// 防Sql注入攻擊
/// </summary>
/// <param name="strQueryName"></param>
/// <returns></returns>
public static string getSQLString(string strQueryName)
{
if (strQueryName == null) return "";
strQueryName = strQueryName.Replace("'", "’").Replace("--", "—").Replace("=", "=").Replace("or", "o r").Replace("Or", "O r").Replace("OR", "O R").Replace("oR", "o R").Replace(">", "〉").Replace("<", "〈");
return strQueryName;
}
#endregion
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.