C#隐藏手机号、邮箱等敏感信息的实现方法
程序员文章站
2022-11-01 14:08:47
intro
做项目的时候,页面上有一些敏感信息,需要用“*”隐藏一些比较重要的信息,于是打算写一个通用的方法。
let's do it !
method 1:指定左右...
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#隐藏手机号、邮箱等敏感信息的实现方法,希望对大家有所帮助