DotNet隐藏敏感信息操作方法
在项目中,有时候一些信息不便完全显示,只需要显示部分信息。现在提供一些隐藏敏感信息的操作方法,主要为对信息的截取操作:
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))
{
throw new ArgumentNullException(info);
}
var sbText = new StringBuilder();
var hiddenCharCount = info.Length - left - right;
if (hiddenCharCount > )
{
string prefix = info.Substring(, left), suffix = info.Substring(info.Length - right);
sbText.Append(prefix);
for (var i = ; i < hiddenCharCount; i++)
{
sbText.Append("*");
}
sbText.Append(suffix);
}
else
{
if (basedOnLeft)
{
if (info.Length > left && left > )
{
sbText.Append(info.Substring(, left) + "****");
}
else
{
sbText.Append(info.Substring(, ) + "****");
}
}
else
{
if (info.Length > right && right > )
{
sbText.Append("****" + info.Substring(info.Length - right));
}
else
{
sbText.Append("****" + info.Substring(info.Length - ));
}
}
}
return sbText.ToString();
}
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))
{
throw new ArgumentNullException(info);
}
var sbText = new StringBuilder();
var hiddenCharCount = info.Length - left - right;
if (hiddenCharCount > )
{
string prefix = info.Substring(, left), suffix = info.Substring(info.Length - right);
sbText.Append(prefix);
sbText.Append("****");
sbText.Append(suffix);
}
else
{
if (basedOnLeft)
{
if (info.Length > left && left > )
{
sbText.Append(info.Substring(, left) + "****");
}
else
{
sbText.Append(info.Substring(, ) + "****");
}
}
else
{
if (info.Length > right && right > )
{
sbText.Append("****" + info.Substring(info.Length - right));
}
else
{
sbText.Append("****" + info.Substring(info.Length - ));
}
}
}
return sbText.ToString();
}
3.“*”数量一定,设置为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 = , bool basedOnLeft = true)
{
if (string.IsNullOrEmpty(info))
{
throw new ArgumentNullException(info);
}
if (sublen <= )
{
sublen = ;
}
var subLength = info.Length / sublen;
if (subLength > && info.Length > (subLength * ))
{
string prefix = info.Substring(, subLength), suffix = info.Substring(info.Length - subLength);
return prefix + "****" + suffix;
}
if (basedOnLeft)
{
var prefix = subLength > ? info.Substring(, subLength) : info.Substring(, );
return prefix + "****";
}
var suffixs = subLength > ? info.Substring(info.Length - subLength) : info.Substring(info.Length - );
return "****" + suffixs;
}
4.隐藏右键详情
/// <summary>
/// 隐藏右键详情
/// </summary>
/// <param name="email">邮件地址</param>
/// <param name="left">邮件头保留字符个数,默认值设置为3</param>
/// <returns></returns>
public static string HideEmailDetails(string email, int left = )
{
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);
}
在一些信息的隐藏操作,可以采用js实现,也可以采用jquery插件完成操作,但是在前台进行这样的操作,存在一些风险,在后台完成对字符的信息截取,可以很好的对信息进行保护。
DotNet隐藏敏感信息操作方法的更多相关文章
- C#实现隐藏手机号、邮箱、姓名等敏感信息扩展方法
还是老惯例,直接上代码. 最终效果图: public static class HideSensitiveInfoExtension { /// <summary> /// 隐藏敏感信息 ...
- Web应用程序的敏感信息-隐藏目录和文件
Web应用程序的敏感信息-隐藏目录和文件 0x1.场景 Web应用程序根文件夹中可能存在大量隐藏信息:源代码版本系统文件夹和文件(.git,.gitignore,.svn),项目配置文件(.npmrc ...
- window下隐藏apache版本和PHP脚本等敏感信息
隐藏Apache信息 1.1 主配置中启用httpd-default.conf 文件: conf/httpd.Conf 找到httpd-default.conf,删除前面的注释“#”,改成如下 Inc ...
- 使用jni技术进行android应用签名信息核查及敏感信息保护
近期业余时间写了一款应用<摇啊摇>,安智.安卓.360等几个应用商店已经陆续审核通过并上线.从有想法到终于将产品做出来并公布,断断续续花了近二个半月的业余时间,整体来讲还算顺 ...
- 保护 .NET Core 项目的敏感信息
我们的项目中几乎都会有配置文件,里面可能会存储一些敏感信息,比如数据库连接字符串.第三方 API 的 AppKey 和 SecretKey 等. 对于开源项目,这些敏感信息肯定不能随着源代码一起提交到 ...
- The server of Apache (四)——配置防盗链和隐藏版本信息
一.防盗链 防盗链就是防止别人的网站代码里面盗用我们服务器的图片.文件.视频等相关资源,比如我们的网页的图片有链接,别人把链接复制粘贴到他们的服务器页面里,图片不在他们自己的网站里,每次打开他们的网站 ...
- 如何删除GIT仓库中的敏感信息
如何删除GIT仓库中的敏感信息 正常Git仓库中应该尽量不包含数据库连接/AWS帐号/巨大二进制文件,否则一旦泄漏到Github,这些非常敏感信息会影响客户的信息安全已经公司的信誉.公司可能其它还有相 ...
- .NET敏感信息分离托管 娓娓道来
引言 互联网每隔一段时间就会爆出 [某程序猿在代码托管平台上传了公司机密配置信息,导致公司核心数据被黑客获取或修改], 一茬又一茬背锅侠层出不穷. 软件工程理论早以加粗字体给出 经典原则:Never ...
- logstash 对配置文件conf敏感信息,密码等加密
logstash的配置文件conf经常会涉及敏感信息,比如ES,mysql的账户密码等,以下使用logstash导入mysql为例子,加密隐藏mysql的密码. 在向keystore中添加key及其s ...
随机推荐
- Redis 排行榜 相同分数根据时间优先排行
版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 需求 Redis 提供了按分数进行排序的有序集合. 比如在游戏里面,比如战斗力排行,充值排行,用默认的Redis 实现就可以达到需求. ...
- js将数字转成大写中文
<script type="text/javascript"> //主函数 function DX(n) { if (!/^(0|[1-9]\d*)(\.\d+)?$/ ...
- 用微信小程序开发的Canvas绘制可配置的转盘抽奖
使用https://github.com/givebest/GB-canvas-turntable代码移植过而来. 其它 微信小程序感觉是个半成品,代码移植过程比较繁琐麻烦.canvas API 部分 ...
- C#模拟http 发送post或get请求
/// <summary> /// 模拟HTTP提交表单并获取返回数据 /// POST /// </summary> /// <param name="Url ...
- Lesson 13 The Greenwood Boys
Text The Greenwood Boys are group of pop singers. At present, they are visiting all parts of the cou ...
- Linux sudo
200 ? "200px" : this.width)!important;} --> 介绍 本篇文章主要介绍sudo配置和用法,为了给某个用户控制权限比如执行某个命令或者关 ...
- 小型单文件NoSQL数据库SharpFileDB初步实现
小型单文件NoSQL数据库SharpFileDB初步实现 我不是数据库方面的专家,不过还是想做一个小型的数据库,算是一种通过mission impossible进行学习锻炼的方式.我知道这是自不量力, ...
- [ASP.NET MVC 小牛之路]16 - Model 验证
上一篇博文 [ASP.NET MVC 小牛之路]15 - Model Binding 中讲了MVC在Model Binding过程中如何根据用户提交HTTP请求数据创建Model对象.在实际的项目中, ...
- php单条件查询,关键字查询
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Atitit.自然语言处理--摘要算法---圣经章节旧约39卷概览bible overview v2 qa1.docx
Atitit.自然语言处理--摘要算法---圣经章节旧约39卷概览bible overview v2 qa1.docx 1. 摘要算法的大概流程2 2. 旧约圣经 (39卷)2 2.1. 与古兰经的对 ...