DotNet and dotnet

Source: Internet
Author: User

DotNet and dotnet

In projects, sometimes some information cannot be fully displayed, but only some information needs to be displayed. Currently, some operations are provided to hide sensitive information, mainly for intercepting information:

1. specify the number of left and right characters. The number of * in the middle is related to the actual length:

/// <Summary> /// hide sensitive information /// </summary> /// <param name = "info"> Information Entity </param> /// <param name = "left"> Number of characters retained on the left </param> // <param name = "right"> Number of characters retained on the right </param> /// <param name = "basedOnLeft"> when the length is abnormal, show left/// <code> true </code> show left, <code> false </code> display the right /// </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 

2. specify the number of left and right characters. The number of * in the middle is fixed:

/// <Summary> /// hide sensitive information /// </summary> /// <param name = "info"> Information Entity </param> /// <param name = "left"> Number of characters retained on the left </param> // <param name = "right"> Number of characters retained on the right </param> /// <param name = "basedOnLeft"> when the length is abnormal, show left/// <code> true </code> show left, <code> false </code> display the right // <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. Set the number of "*" to 4, which is calculated based on the ratio of the total length of the information. The default value is 1/3 for each left and right:

/// <Summary> /// hide sensitive information /// </summary> /// <param name = "info"> Information </param> /// <param name = "sublen"> total length of information and left substring (or right substring) </param> /// <param name = "basedOnLeft"/> If the length is abnormal, whether to display the value on the left. The default value is true, left is displayed by default. // <code> true </code> is displayed on the left, <code> false </code> display the right // <returns> </returns> public static string HideSensitiveInfo (string info, int sublen = 3, bool basedOnLeft = true) {if (string. isNullOrEmpty (info) {throw new Argum EntNullException (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 ;}

4. Hide right-click Details

/// <Summary> /// hide the right-click Details /// </summary> /// <param name = "email"> email address </param> /// <param name = "left"> Number of reserved characters in the mail header, the default value is 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 );}

Some information hiding operations can be implemented using js or jquery plug-ins. However, there are some risks in performing such operations on the foreground, intercepting character information in the background can effectively protect the information.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.