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

Intro

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

Let's do it !

Method 1:指定左右字符数量

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

/// <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 : 中间的*的个数固定

/// <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

/// <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

/// <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

/// <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);
}

测试结果如下:

邮件地址

/// <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
, 隐藏敏感信息
, c邮箱敏感信息
c手机号敏感信息
c站、c语言、cf、ch、c罗,以便于您获取更多的相关知识。

时间: 2024-08-03 13:33:40

C#隐藏手机号、邮箱等敏感信息的实现方法_C#教程的相关文章

C#抓取网页数据 解析标题描述图片等信息 去除HTML标签_C#教程

一.首先将网页内容整个抓取下来,数据放在byte[]中(网络上传输时形式是byte),进一步转化为String,以便于对其操作,实例如下: 复制代码 代码如下: private static string GetPageData(string url) {     if (url == null || url.Trim() == "")         return null;     WebClient wc = new WebClient();     wc.Credentials

C#编程实现动态改变配置文件信息的方法_C#教程

本文实例讲述了C#编程实现动态改变配置文件信息的方法.分享给大家供大家参考,具体如下: 配置文件实际上就是一个XML文件,所以我们可以使用XmlDocument来进行操作. 代码如下: static void Main(string[] args) { XmlDocument xDoc = new XmlDocument(); xDoc.Load("../../App.config");//加载xml文件 XmlNode xNode; XmlElement xElem1; XmlEle

在C#中根据HardwareID获取驱动程序信息的实现代码_C#教程

近日在工作中需要根据设备的HardwareID来获取设备的驱动程序信息,比如驱动程序版本等.经过摸索,得到了两种不同的解决办法,两种办法各有千秋,写出来给大家分享. 1 使用WMI中的Win32_PnPSignedDriver类 Win32_PnPSignedDriver的详细信息:http://msdn2.microsoft.com/en-us/library/aa394354.aspx 使用WMI(Windows Management Instrumentation)是最为方便的方法.可以根

重写、隐藏基类(new, override)的方法_C#教程

复制代码 代码如下: public class Father    {        public void Write() {            Console.WriteLine("父");        }    }     public class Mother    {        public virtual void Write()        {            Console.WriteLine("母");        }    }

C#获取系统版本信息方法_C#教程

直接贴代码: 复制代码 代码如下: public class OSInfoMation { public static string OSBit() { try { ConnectionOptions oConn = new ConnectionOptions(); System.Management.ManagementScope managementScope = new System.Management.ManagementScope("\\\\localhost", oCon

使用ShellClass获取文件属性详细信息的实现方法_C 语言

首先引用COM组件Microsoft Shell Controls And Automation这里需要注意DLL的属性Embed Interop Type 设为False否则会引起互操作类型异常 代码如下ShellClass sh = new ShellClass();Folder dir = sh.NameSpace(Path.GetDirectoryName(sFile));FolderItem item = dir.ParseName(Path.GetFileName(sFile));s

12306再曝重大漏洞:登陆密码等敏感信息会泄露

今天是圣诞节,本该是一个非常和谐的日子,但12306又不给力了.乌云网今天上午出现了一则关于12306的漏洞报告,危害等级显示为高,漏洞类型则是用户资料大量泄漏.具体来说,这个漏洞会导致12306用户的账号.明文密码.身份证.邮箱等敏感信息泄露,而泄漏的途径目前还不知道.目前该漏洞已经提交给了国家互联网应急中心进行处理,暂无进一步消息.

如何隐藏Apache版本号和其它敏感信息

当远程请求发送到你的 Apache Web 服务器时,在默认情况下,一些有价值的信息,如 web 服务器版本号.服务器操作系统详细信息.已安装的 Apache 模块等等,会随服务器生成的文档发回客户端. 这给攻击者利用漏洞并获取对 web 服务器的访问提供了很多有用的信息.为了避免显示 web 服务器信息,我们将在本文中演示如何使用特定的 Apache 指令隐藏 Apache Web 服务器的信息. 两个重要的指令是: ServerSignature 这允许在服务器生成的文档(如错误消息.mod

个人敏感信息打码,你使用的产品真正做到位了吗?

本文讲的是个人敏感信息打码,你使用的产品真正做到位了吗?,上周五58同城被爆简历数据泄漏,有淘宝店家正在贩卖,只需700元就可购买软件采集全国用户的简历信息,包括姓名.手机.年龄.求职方向.期望月薪.工作经验.居住地.学历等. 招聘行业一直是信息泄漏高风险区,58同城此次事件也印证了这点.但略难堪的是,2016年初开始传播.持续一年多时间的成规模简历泄漏,只是因为几个接口设计不当的低级漏洞,不禁令人反思. 最近两天,嘶吼编辑和接近此事的朋友聊天,侧面了解到一则关联的信息泄漏漏洞.由于58同城内部