• 加密方法


    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, @"([\r\n])[\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("\r\n", "");
                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]|1\d|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("\r\n", "<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
        }
  • 相关阅读:
    Request.ServerVariables
    asp.net 枚举
    MSSQL批量替换语句 在SQL SERVER中批量修改替换数据
    由于 ASP.NET 进程标识对全局程序集缓存没有读权限,因此未能执行请求
    mongodb 下载地址,mongodb.dll 下载
    mongodb 常用操作(转)
    mssql 列出数据库中的所有表
    jpg图片在火狐中和ie中格式区别
    Ndo 新版本发布
    消息总线设计系列之 调停者模式
  • 原文地址:https://www.cnblogs.com/chaoa/p/2331328.html
Copyright © 2020-2023  润新知