Implementation Code of md5 encryption in asp.net

Source: Internet
Author: User
Tags convert text to html

This article introduces the implementation of md5 encryption, which also includes SQL anti-injection code. If you need it, you can refer to it.

The Code is as follows: Copy code

Public class FormatStr
{
# Region MD5 Encryption
/// <Summary>
/// MD5 Encryption
/// </Summary>
/// <Param name = "str"> encrypted string </param>
/// <Returns> </returns>
Public static string MD5 (string str ){
Return System. Web. Security. FormsAuthentication. HashPasswordForStoringInConfigFile (str, "MD5 ");
}
# Endregion

# Region remove HTML tags
/// <Summary>
/// Remove HTML tags
/// </Summary>
/// <Param name = "NoHTML"> including the source code of HTML </param>
/// <Returns> removed text </returns>
Public static string NoHTML (string Htmlstring)
{
// Delete the script
Htmlstring = Regex. Replace (Htmlstring, @ "<script [^>] *?>. *? </Script> "," ", RegexOptions. IgnoreCase );
// Delete 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 Remove invalid strings
/// <Summary>
/// Remove invalid strings
/// </Summary>
/// <Param name = "strChar"> original string </param>
/// <Returns> filtered strings </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

# Check whether region is a number
/// <Summary>
/// Check whether all items are numbers
/// </Summary>
/// <Param name = "str"> string to be checked </param>
/// <Returns> bool </returns>
Public static bool IsNumeric (string str)
{
Regex reg = new Regex (@ "^ [+]? D * $ ");
Return reg. IsMatch (str );
}
# Endregion

# Check whether the Email format is correct for region
/// <Summary>
/// Check whether the Email format is correct
/// </Summary>
/// <Param name = "str"> string to be checked </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

# Check whether the date format is correct for region
/// <Summary>
/// Check whether the date format is correct
/// </Summary>
/// <Param name = "str"> string to be checked </param>
/// <Returns> bool </returns>
Public static bool IsDate (string str)
{
// Take into account the 366 day of the 4-year period, and there is a special February date
Regex reg = new Regex (@ "^ (1 [6-9] | [2-9] d) d {2})-(0? [1, 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 conversion to a string
/// <Summary>
/// Convert HTML to a string and the conversion identifier
/// </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 String Conversion to HTML
/// <Summary>
/// Convert the string to HTML and restore the ID
/// </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

# Convert region text to HTML
/// <Summary>
/// Convert text to HTML, replace (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

# Convert region text to input box content
/// <Summary>
/// Convert the text into the content of the input box,
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static string HtmToInputValue (string str)
{
// Return str. replace ("<", "<"). replace (">", "> "). replace (""","""). replace ("&","&");
Return str. Replace (""",""");
}
# Endregion

# Region protection against SQL injection attacks
/// <Summary>
/// Prevents SQL injection attacks
/// </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 protection against SQL injection attacks
/// <Summary>
/// Prevents SQL injection attacks
/// </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
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.