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); } |
測試結果如下:
手機號 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); } |
測試結果如下:
郵件地址
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#隱藏手機號、郵箱等敏感信息的實現方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的,在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/ben121011/archive/2016/09/14/hideSensitiveInfoViaCSharp.html