C#隐藏手机号、邮箱等敏感信息的实现方法

时间:2022-06-25 06:52:09

intro

做项目的时候,页面上有一些敏感信息,需要用“*”隐藏一些比较重要的信息,于是打算写一个通用的方法。

let's do it !

method 1:指定左右字符数量

method 1.1 中间的*的个数和实际长度有关

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// <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))
{
return "";
}
stringbuilder sbtext = new stringbuilder();
int hiddencharcount = info.length - left - right;
if (hiddencharcount > 0)
{
string prefix = info.substring(0, left), suffix = info.substring(info.length - right);
sbtext.append(prefix);
for (int 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 1.2 : 中间的*的个数固定

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// <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))
{
return "";
}
stringbuilder sbtext = new stringbuilder();
int 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 2 : “*”数量一定,设置为4个,按信息总长度的比例来取,默认左右各取1/3

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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))
{
return "";
}
if (sublen<=1)
{
sublen = 3;
}
int 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;
}
else
{
if (basedonleft)
{
string prefix = sublength > 0 ? info.substring(0, sublength) : info.substring(0, 1);
return prefix + "****";
}
else
{
string suffix = sublength > 0 ? info.substring(info.length-sublength) : info.substring(info.length-1);
return "****"+suffix;
}
}
}

扩展

手机号 1

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

测试结果如下:

C#隐藏手机号、邮箱等敏感信息的实现方法

手机号 2

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

测试结果如下:

C#隐藏手机号、邮箱等敏感信息的实现方法

邮件地址

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <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))
{
return "";
}
if (system.text.regularexpressions.regex.ismatch(email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))//如果是邮件地址
{
int suffixlen =email.length - email.lastindexof('@');
return hidesensitiveinfo(email, left, suffixlen,false);
}
else
{
return hidesensitiveinfo(email);
}
}

测试结果如下:

C#隐藏手机号、邮箱等敏感信息的实现方法

以上所述是小编给大家介绍的c#隐藏手机号、邮箱等敏感信息的实现方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/ben121011/archive/2016/09/14/hideSensitiveInfoViaCSharp.html