一周小代码秀09/10~09/15

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
/// <summary>
///RequestHelper过滤获取传入的值
///by zjf兔子
/// </summary>
public class RequestHelper
{

    #region SQL 特殊字符过滤,防SQL注入
    public static string SqlFilter(string Contents)
    {
        string _pattern = "exec|insert|select|delete|'|update|chr|mid|master|truncate|char|declare|and|--";
        if (Regex.IsMatch(Contents.ToLower(), _pattern, RegexOptions.IgnoreCase))
        {
            Contents = Regex.Replace(Contents.ToLower(), _pattern, " ", RegexOptions.IgnoreCase);
        }
        return Contents;
    }
    #endregion

    #region  获得传入对象的值(返回字符串类型)
    public static string GetInputString(string _value, string defaultValue, bool filterSQL, bool filterHtml)
    {

        if (string.IsNullOrEmpty(_value))
        {
            return defaultValue;
        }
        if (filterSQL)
        {
            _value = SqlFilter(_value);
        }
        if (filterHtml)
        {
            _value = StripTags(_value);
        }
        return _value;
    }
    #endregion

    #region 过滤所有HTML标签
    public static string StripTags(string input)
    {
        Regex regex = new Regex("<([^<]|\n)+?>");
        return regex.Replace(input, "");
    }
    #endregion

    #region 获得传入对象的值(返回整型)
    public static int GetInputInt32(string _value, int defaultValue)
    {

        int num;
        if (!int.TryParse(_value, out num))
        {
            num = defaultValue;
        }
        return num;
    }
    #endregion

    #region 获得返回的键值
    public static string GetKeyValue(string str, string key)
    {

        string regstr = @"{" + key.Trim() + "}(.*){/" + key.Trim() + "}";
        //if (str.Contains('?'))
        //{
        //    regstr = @"{" + key.Trim() + "}(.*){/" + key.Trim() + "}";
        //}
        //else
        //{
        //    regstr = @"{" + key.Trim() + "}(\\w*){/" + key.Trim() + "}";
        //}
        Match m = Regex.Match(str, regstr, RegexOptions.IgnoreCase);
        if (m.Success)
        {
            string val = m.Groups[1].Value;
            if (string.IsNullOrEmpty(val))
            {
                return "";
            }
            else
            {
                return val;
            }
        }
        else
        {
            return "error";
        }

    }
    #endregion

    #region 获得返回的键值
        /**
                  *针对手机绑定+问题绑定返回的状态
                  *返回的格式{state}1{/state},{s}该手机已经绑定{/s}等类似的值。。。。
                  */
    public static string GetKeyValue(string str, params string[] key)
    {
        string retvalue = string.Empty;
        for (int i = 0; i < key.Length; i++)
        {
            string regstr = @"{" + key[i].Trim() + "}(.*){/" + key[i].Trim() + "}";
            Match m = Regex.Match(str, regstr, RegexOptions.IgnoreCase);
            if (m.Success)
            {
                string val = m.Groups[1].Value;
                if (string.IsNullOrEmpty(val))
                {
                    retvalue += "empty,";
                }
                else
                {
                    retvalue += val + ",";
                }
            }
            else
            {
                return retvalue += "error,";
            }
        }
        return retvalue;
    }
    #endregion

    /// <summary>
    /// Md5 GB2312
    /// </summary>
    /// <param name="ConvertString"></param>
    /// <returns></returns>
    public static string MD5(string ConvertString)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        string t2 = BitConverter.ToString(md5.ComputeHash(System.Text.UTF8Encoding.Default.GetBytes(ConvertString)), 0, 16);
        md5.Clear();
        t2 = t2.Replace("-", "");
        return t2.ToLower();
    }

    public static string Md5_32(string str)
    {
        string cl = str;
        string pwd = "";
        System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();//实例化一个md5对像
        // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
        byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
        // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
        for (int i = 0; i < s.Length; i++)
        {
            // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符

            pwd = pwd + s[i].ToString("X");

        }
        return pwd;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值