English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Implementation Method of Hiding Sensitive Information Such as Phone Number and Email in C#

Intro

When doing a project, there are some sensitive information on the page that needs to be used with "*"I want to hide some important information, so I plan to write a general method.

Let's do it !

Method 1: Specify the number of characters on both sides

Method 1.1 in the middle*The number of characters and the actual length are related

/// <summary>
/// Hide sensitive information
/// </summary>
/// <param name="info">Information entity</param>
/// <param name="left">The number of characters retained on the left</param>
/// <param name="right">The number of characters retained on the right</param>
/// <param name="basedOnLeft">Whether to display the left side when the length is abnormal 
/// <code>true</code>/code>Display on the left, <code>false</code>/code>Display on the right
/// </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 : The middle*The number of fixed

/// <summary>
/// Hide sensitive information
/// </summary>
/// <param name="info">Information entity</param>
/// <param name="left">The number of characters retained on the left</param>
/// <param name="right">The number of characters retained on the right</param>
/// <param name="basedOnLeft">Whether to display the left side when the length is abnormal 
/// <code>true</code>/code>Display on the left, <code>false</code>/code>Display on the right
/// <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 : "*"The number is fixed, set to4each, according to the ratio of the total length of the information, the default is to take from both sides1/3

/// <summary>
/// Hide sensitive information
/// </summary>
/// <param name="info">Information</param>
/// <param name="sublen">The ratio of total length of information to left substring (or right substring)</param>
/// <param name="basedOnLeft">Whether to display the left side when the length is abnormal, default true, default display the left side</param>
/// <code>true</code>/code>Display on the left, <code>false</code>/code>Display on the right
/// <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 &&63; info.Substring(0, subLength) : info.Substring(0, 1);
return prefix + "****";
}
else
{
string suffix = subLength > 0 &&63; info.Substring(info.Length-subLength) : info.Substring(info.Length-1);
return "****"+suffix;
}
}
}

Expansion

Phone number 1

/// <summary>
/// Hide phone number details
/// </summary>
/// <param name="phone">Phone number</param>
/// <param name="left">Number of characters retained on the left</param>
/// <param name="right">Number of characters retained on the right</param>
/// <returns></returns>
public static string HideTelDetails(string phone, int left = 3, int right = 4)
{
return HideSensitiveInfo(phone, left, right);
}

The test results are as follows:

Phone number 2

/// <summary>
/// Hide phone number details
/// </summary>
/// <param name="phone">Phone number</param>
/// <param name="left">Number of characters retained on the left</param>
/// <param name="right">Number of characters retained on the right</param>
/// <returns></returns>
public static string HideTelDetails(string phone, int left = 3, int right = 4)
{
return HideSensitiveInfo1(phone, left, right);
}

The test results are as follows:

Email address

/// <summary>
/// Hide right-click details
/// </summary>
/// <param name="email">Email address</param>
/// <param name="left">Number of characters retained in the email header, the default value is set to3</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+)*"))//If it is an email address
{
int suffixLen = email.Length - email.LastIndexOf('@');
return HideSensitiveInfo(email, left, suffixLen, false);
}
else
{
return HideSensitiveInfo(email);
}
}

The test results are as follows:

The above-mentioned methods introduced by the editor to hide sensitive information such as mobile phone numbers and email addresses in C# are for your reference. I hope they will be helpful to you. If you have any questions, please leave me a message, and I will reply to you in time. Thank you very much for your support of the Yell Tutorial website!

Statement: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting via email, please replace # with @) for reporting violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like