dotnet隐藏操作方法,邮箱等敏感音信

   
在类型中,有时候有些音信不方便完全展示,只须求出示部分音讯。今后提供部分隐藏敏感音讯的操作方法,首要为对音讯的截取操作:

DotNet隐藏敏感新闻操作方法,dotnet隐藏操作方法

   
在项目中,有时候某些新闻不方便完全显示,只供给浮现部分新闻。未来提供一些隐藏敏感新闻的操作方法,首要为对消息的截取操作:

     壹.钦命左右字符数量,中间的*的个数和实际尺寸有关:

        /// <summary>
        /// 隐藏敏感信息
        /// </summary>
        /// <param name="info">信息实体</param>
        /// <param name="left">左边保留的字符数</param>
        /// <param name="right">右边保留的字符数</param>
        /// <param name="basedOnLeft">当长度异常时,是否显示左边 
        /// <code>true</code>显示左边,<code>false</code>显示右边
        /// </param>
        /// <returns></returns>
        public static string HideSensitiveInfo(string info, int left, int right, bool basedOnLeft = true)
        {
            if (string.IsNullOrEmpty(info))
            {
                throw new ArgumentNullException(info);
            }
            var sbText = new StringBuilder();
            var hiddenCharCount = info.Length - left - right;
            if (hiddenCharCount > 0)
            {
                string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
                sbText.Append(prefix);
                for (var i = 0; i < hiddenCharCount; i++)
                {
                    sbText.Append("*");
                }
                sbText.Append(suffix);
            }
            else
            {
                if (basedOnLeft)
                {
                    if (info.Length > left && left > 0)
                    {
                        sbText.Append(info.Substring(0, left) + "****");
                    }
                    else
                    {
                        sbText.Append(info.Substring(0, 1) + "****");
                    }
                }
                else
                {
                    if (info.Length > right && right > 0)
                    {
                        sbText.Append("****" + info.Substring(info.Length - right));
                    }
                    else
                    {
                        sbText.Append("****" + info.Substring(info.Length - 1));
                    }
                }
            }
            return sbText.ToString();
        }

    二.钦定左右字符数量,中间的*的个数固定:

        /// <summary>
        /// 隐藏敏感信息
        /// </summary>
        /// <param name="info">信息实体</param>
        /// <param name="left">左边保留的字符数</param>
        /// <param name="right">右边保留的字符数</param>
        /// <param name="basedOnLeft">当长度异常时,是否显示左边 
        /// <code>true</code>显示左边,<code>false</code>显示右边
        /// <returns></returns>
        public static string HideSensitiveInfo1(string info, int left, int right, bool basedOnLeft = true)
        {
            if (string.IsNullOrEmpty(info))
            {
                throw new ArgumentNullException(info);
            }
            var sbText = new StringBuilder();
            var hiddenCharCount = info.Length - left - right;
            if (hiddenCharCount > 0)
            {
                string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
                sbText.Append(prefix);
                sbText.Append("****");
                sbText.Append(suffix);
            }
            else
            {
                if (basedOnLeft)
                {
                    if (info.Length > left && left > 0)
                    {
                        sbText.Append(info.Substring(0, left) + "****");
                    }
                    else
                    {
                        sbText.Append(info.Substring(0, 1) + "****");
                    }
                }
                else
                {
                    if (info.Length > right && right > 0)
                    {
                        sbText.Append("****" + info.Substring(info.Length - right));
                    }
                    else
                    {
                        sbText.Append("****" + info.Substring(info.Length - 1));
                    }
                }
            }
            return sbText.ToString();
        }

    3.“*”数量分明,设置为多少个,按信息总委员长度的比重来取,暗中认可左右各取1/三:

        /// <summary>
        /// 隐藏敏感信息
        /// </summary>
        /// <param name="info">信息</param>
        /// <param name="sublen">信息总长与左子串(或右子串)的比例</param>
        /// <param name="basedOnLeft"/>当长度异常时,是否显示左边,默认true,默认显示左边
        /// <code>true</code>显示左边,<code>false</code>显示右边
        /// <returns></returns>
        public static string HideSensitiveInfo(string info, int sublen = 3, bool basedOnLeft = true)
        {
            if (string.IsNullOrEmpty(info))
            {
                throw new ArgumentNullException(info);
            }
            if (sublen <= 1)
            {
                sublen = 3;
            }
            var subLength = info.Length / sublen;
            if (subLength > 0 && info.Length > (subLength * 2))
            {
                string prefix = info.Substring(0, subLength), suffix = info.Substring(info.Length - subLength);
                return prefix + "****" + suffix;
            }
            if (basedOnLeft)
            {
                var prefix = subLength > 0 ? info.Substring(0, subLength) : info.Substring(0, 1);
                return prefix + "****";
            }
            var suffixs = subLength > 0 ? info.Substring(info.Length - subLength) : info.Substring(info.Length - 1);
            return "****" + suffixs;
        }

   四.隐形右键详细情况

        /// <summary>
        /// 隐藏右键详情
        /// </summary>
        /// <param name="email">邮件地址</param>
        /// <param name="left">邮件头保留字符个数,默认值设置为3</param>
        /// <returns></returns>
        public static string HideEmailDetails(string email, int left = 3)
        {
            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentNullException(email);
            }
            if (!System.Text.RegularExpressions.Regex.IsMatch(email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
                return HideSensitiveInfo(email);
            var suffixLen = email.Length - email.LastIndexOf('@');
            return HideSensitiveInfo(email, left, suffixLen, false);
        }

   
在局地新闻的隐没操作,可以运用js达成,也得以运用jquery插件落成操作,但是在前台举办如此的操作,存在部分风险,在后台完结对字符的新闻截取,可以很好的对音讯举行维护。

在等级次序中,有时候有个别新闻不方便完全显示,只需求显示部分音讯。今后提供一些隐藏敏感信…

dotnet隐藏操作方法,邮箱等敏感音信。隐藏手提式有线电电话机号、邮箱等敏感音讯

     一.点名左右字符数量,中间的*的个数和实在尺寸有关:

Intro

做项目的时候,页面上有一些灵动新闻,供给用“*”隐藏一些比较根本的信息,于是筹算写八个通用的不2秘籍。

Let’s do it !

        /// <summary>
        /// 隐藏敏感信息
        /// </summary>
        /// <param name="info">信息实体</param>
        /// <param name="left">左边保留的字符数</param>
        /// <param name="right">右边保留的字符数</param>
        /// <param name="basedOnLeft">当长度异常时,是否显示左边 
        /// <code>true</code>显示左边,<code>false</code>显示右边
        /// </param>
        /// <returns></returns>
        public static string HideSensitiveInfo(string info, int left, int right, bool basedOnLeft = true)
        {
            if (string.IsNullOrEmpty(info))
            {
                throw new ArgumentNullException(info);
            }
            var sbText = new StringBuilder();
            var hiddenCharCount = info.Length - left - right;
            if (hiddenCharCount > 0)
            {
                string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
                sbText.Append(prefix);
                for (var i = 0; i < hiddenCharCount; i++)
                {
                    sbText.Append("*");
                }
                sbText.Append(suffix);
            }
            else
            {
                if (basedOnLeft)
                {
                    if (info.Length > left && left > 0)
                    {
                        sbText.Append(info.Substring(0, left) + "****");
                    }
                    else
                    {
                        sbText.Append(info.Substring(0, 1) + "****");
                    }
                }
                else
                {
                    if (info.Length > right && right > 0)
                    {
                        sbText.Append("****" + info.Substring(info.Length - right));
                    }
                    else
                    {
                        sbText.Append("****" + info.Substring(info.Length - 1));
                    }
                }
            }
            return sbText.ToString();
        }

Method 一:钦定左右字符数量

    二.点名左右字符数量,中间的*的个数固定:

Method 1.1 中间的*的个数和实在尺寸有关

 1     /// <summary>
 2     /// 隐藏敏感信息
 3     /// </summary>
 4     /// <param name="info">信息实体</param>
 5     /// <param name="left">左边保留的字符数</param>
 6     /// <param name="right">右边保留的字符数</param>
 7     /// <param name="basedOnLeft">当长度异常时,是否显示左边 
 8     /// <code>true</code>显示左边,<code>false</code>显示右边
 9     /// </param>
10     /// <returns></returns>
11     public static string HideSensitiveInfo(string info, int left, int right, bool basedOnLeft=true)
12     {
13         if (String.IsNullOrEmpty(info))
14         {
15             return "";
16         }
17         StringBuilder sbText = new StringBuilder();
18         int hiddenCharCount = info.Length - left - right;
19         if (hiddenCharCount > 0)
20         {
21             string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
22             sbText.Append(prefix);
23             for (int i = 0; i < hiddenCharCount; i++)
24             {
25                 sbText.Append("*");
26             }
27             sbText.Append(suffix);
28         }
29         else
30         {
31             if (basedOnLeft)
32             {
33                 if (info.Length > left && left > 0)
34                 {
35                     sbText.Append(info.Substring(0, left) + "****");
36                 }
37                 else
38                 {
39                     sbText.Append(info.Substring(0, 1) + "****");
40                 }
41             }
42             else
43             {
44                 if (info.Length > right && right > 0)
45                 {
46                     sbText.Append("****" + info.Substring(info.Length - right));
47                 }
48                 else
49                 {
50                     sbText.Append("****" + info.Substring(info.Length - 1));
51                 }
52             }
53         }
54         return sbText.ToString();
55     }

 
        /// <summary>
        /// 隐藏敏感信息
        /// </summary>
        /// <param name="info">信息实体</param>
        /// <param name="left">左边保留的字符数</param>
        /// <param name="right">右边保留的字符数</param>
        /// <param name="basedOnLeft">当长度异常时,是否显示左边 
        /// <code>true</code>显示左边,<code>false</code>显示右边
        /// <returns></returns>
        public static string HideSensitiveInfo1(string info, int left, int right, bool basedOnLeft = true)
        {
            if (string.IsNullOrEmpty(info))
            {
                throw new ArgumentNullException(info);
            }
            var sbText = new StringBuilder();
            var hiddenCharCount = info.Length - left - right;
            if (hiddenCharCount > 0)
            {
                string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
                sbText.Append(prefix);
                sbText.Append("****");
                sbText.Append(suffix);
            }
            else
            {
                if (basedOnLeft)
                {
                    if (info.Length > left && left > 0)
                    {
                        sbText.Append(info.Substring(0, left) + "****");
                    }
                    else
                    {
                        sbText.Append(info.Substring(0, 1) + "****");
                    }
                }
                else
                {
                    if (info.Length > right && right > 0)
                    {
                        sbText.Append("****" + info.Substring(info.Length - right));
                    }
                    else
                    {
                        sbText.Append("****" + info.Substring(info.Length - 1));
                    }
                }
            }
            return sbText.ToString();
        }

Method 1.2 : 中间的*的个数固定

 1     /// <summary>
 2     /// 隐藏敏感信息
 3     /// </summary>
 4     /// <param name="info">信息实体</param>
 5     /// <param name="left">左边保留的字符数</param>
 6     /// <param name="right">右边保留的字符数</param>
 7     /// <param name="basedOnLeft">当长度异常时,是否显示左边 
 8     /// <code>true</code>显示左边,<code>false</code>显示右边
 9     /// <returns></returns>
10     public static string HideSensitiveInfo1(string info, int left, int right, bool basedOnLeft = true)
11     {
12         if (String.IsNullOrEmpty(info))
13         {
14             return "";
15         }
16         StringBuilder sbText = new StringBuilder();
17         int hiddenCharCount = info.Length - left - right;
18         if (hiddenCharCount > 0)
19         {
20             string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
21             sbText.Append(prefix);
22             sbText.Append("****");
23             sbText.Append(suffix);
24         }
25         else
26         {
27             if (basedOnLeft)
28             {
29                 if (info.Length > left && left >0)
30                 {
31                     sbText.Append(info.Substring(0, left) + "****");
32                 }
33                 else
34                 {
35                     sbText.Append(info.Substring(0, 1) + "****");
36                 }
37             }
38             else
39             {
40                 if (info.Length > right && right>0)
41                 {
42                     sbText.Append("****" + info.Substring(info.Length - right));
43                 }
44                 else
45                 {
46                     sbText.Append("****" + info.Substring(info.Length - 1));
47                 }
48             }
49         }
50         return sbText.ToString();
51     }

亚洲必赢官网 , 

    3.“*”数量明确,设置为两个,按音讯总司长度的百分比来取,暗许左右各取1/三:

Method 2 : “*”数量分明,设置为5个,按音讯总院长度的比重来取,暗中同意左右各取1/三

 1     /// <summary>
 2     /// 隐藏敏感信息
 3     /// </summary>
 4     /// <param name="info">信息</param>
 5     /// <param name="sublen">信息总长与左子串(或右子串)的比例</param>
 6     /// <param name="basedOnLeft">当长度异常时,是否显示左边,默认true,默认显示左边
 7     /// <code>true</code>显示左边,<code>false</code>显示右边
 8     /// <returns></returns>
 9     public static string HideSensitiveInfo(string info,int sublen = 3,bool basedOnLeft = true)
10     {
11         if (String.IsNullOrEmpty(info))
12         {
13             return "";
14         }
15         if (sublen<=1)
16         {
17             sublen = 3;
18         }
19         int subLength = info.Length / sublen;
20         if (subLength > 0 && info.Length > (subLength*2) )
21         {
22             string prefix = info.Substring(0, subLength), suffix = info.Substring(info.Length - subLength);
23             return prefix + "****" + suffix;
24         }
25         else
26         {
27             if (basedOnLeft)
28             {
29                 string prefix = subLength > 0 ? info.Substring(0, subLength) : info.Substring(0, 1);
30                 return prefix + "****";
31             }
32             else
33             {
34                 string suffix = subLength > 0 ? info.Substring(info.Length-subLength) : info.Substring(info.Length-1);
35                 return "****"+suffix;
36             }
37         }
38     }
        /// <summary>
        /// 隐藏敏感信息
        /// </summary>
        /// <param name="info">信息</param>
        /// <param name="sublen">信息总长与左子串(或右子串)的比例</param>
        /// <param name="basedOnLeft"/>当长度异常时,是否显示左边,默认true,默认显示左边
        /// <code>true</code>显示左边,<code>false</code>显示右边
        /// <returns></returns>
        public static string HideSensitiveInfo(string info, int sublen = 3, bool basedOnLeft = true)
        {
            if (string.IsNullOrEmpty(info))
            {
                throw new ArgumentNullException(info);
            }
            if (sublen <= 1)
            {
                sublen = 3;
            }
            var subLength = info.Length / sublen;
            if (subLength > 0 && info.Length > (subLength * 2))
            {
                string prefix = info.Substring(0, subLength), suffix = info.Substring(info.Length - subLength);
                return prefix + "****" + suffix;
            }
            if (basedOnLeft)
            {
                var prefix = subLength > 0 ? info.Substring(0, subLength) : info.Substring(0, 1);
                return prefix + "****";
            }
            var suffixs = subLength > 0 ? info.Substring(info.Length - subLength) : info.Substring(info.Length - 1);
            return "****" + suffixs;
        }

扩展

   四.隐形右键详细的情况

手机号 1

 1     /// <summary>
 2     /// 隐藏手机号详情
 3     /// </summary>
 4     /// <param name="phone">手机号</param>
 5     /// <param name="left">左边保留字符数</param>
 6     /// <param name="right">右边保留字符数</param>
 7     /// <returns></returns>
 8     public static string HideTelDetails(string phone, int left = 3, int right = 4)
 9     {
10         return HideSensitiveInfo(phone, left, right);
11     }

 

测试结果如下:

亚洲必赢官网 1

 

        /// <summary>
        /// 隐藏右键详情
        /// </summary>
        /// <param name="email">邮件地址</param>
        /// <param name="left">邮件头保留字符个数,默认值设置为3</param>
        /// <returns></returns>
        public static string HideEmailDetails(string email, int left = 3)
        {
            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentNullException(email);
            }
            if (!System.Text.RegularExpressions.Regex.IsMatch(email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
                return HideSensitiveInfo(email);
            var suffixLen = email.Length - email.LastIndexOf('@');
            return HideSensitiveInfo(email, left, suffixLen, false);
        }

手机号 2

 1     /// <summary>
 2     /// 隐藏手机号详情
 3     /// </summary>
 4     /// <param name="phone">手机号</param>
 5     /// <param name="left">左边保留字符数</param>
 6     /// <param name="right">右边保留字符数</param>
 7     /// <returns></returns>
 8     public static string HideTelDetails(string phone, int left = 3, int right = 4)
 9     {
10         return HideSensitiveInfo1(phone, left, right);
11     }

 

测试结果如下:

亚洲必赢官网 2

 

   
在有个别新闻的隐蔽操作,能够使用js落成,也足以使用jquery插件完毕操作,可是在前台实行如此的操作,存在有的风险,在后台完成对字符的音信截取,能够很好的对音讯进行尊敬。

邮件地址

 1     /// <summary>
 2     /// 隐藏右键详情
 3     /// </summary>
 4     /// <param name="email">邮件地址</param>
 5     /// <param name="left">邮件头保留字符个数,默认值设置为3</param>
 6     /// <returns></returns>
 7     public static string HideEmailDetails(string email, int left = 3)
 8     {
 9         if (String.IsNullOrEmpty(email))
10         {
11             return "";
12         }
13         if (System.Text.RegularExpressions.Regex.IsMatch(email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))//如果是邮件地址
14         {
15             int suffixLen =email.Length - email.LastIndexOf('@');
16             return HideSensitiveInfo(email, left, suffixLen,false);
17         }
18         else
19         {
20             return HideSensitiveInfo(email);
21         }
22     }

 

测试结果如下:

亚洲必赢官网 3

 

网站地图xml地图