zoukankan      html  css  js  c++  java
  • C# 实现数据脱敏

    数据脱敏,此处以姓名、身份证为例
     /// <summary>
            /// 姓名敏感处理
            /// </summary>
            /// <param name="fullName">姓名</param>
            /// <returns>脱敏后的姓名</returns>
            public string SetSensitiveName(string fullName)
            {
                if (string.IsNullOrEmpty(fullName)) return string.Empty;
    
                string familyName = fullName.Substring(0, 1);
                string end = fullName.Substring(fullName.Length - 1, 1);
                string name = string.Empty;
                //长度为2
                if (fullName.Length <= 2) name = familyName + "*";
                //长度大于2
                else if (fullName.Length >= 3)
                {
                    name = familyName.PadRight(fullName.Length - 1, '*') + end;
                }
                return name;
            }
    
            /// <summary>
            /// 身份证脱敏
            /// </summary>
            /// <param name="idCardNo">身份证号</param>
            /// <returns>脱敏后的身份证号</returns>
            private string SetSensitiveIdCardNo(string idCardNo)
            {
                if (string.IsNullOrEmpty(idCardNo)
                    || (idCardNo.Length != 15 && idCardNo.Length != 18)) return idCardNo;
    
                string begin = idCardNo.Substring(0, 6);
                string middle = idCardNo.Substring(6, 8);
                string end = idCardNo.Substring(14, idCardNo.Length - 14);
    
                string card = string.Empty;
                card = begin + "********" + end;
                return card;
            }
    
    测试:
                string no = SetSensitiveIdCardNo("610253199804230067");
                //string no = SetSensitiveIdCardNo("610253199804230");
                string name = SetSensitiveName("齐天大圣孙悟空");
                MessageBox.Show("idcardno==" + no + "
    name==" + name);
    

     

     

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    .Net 集合类
    Linux与Windows共享资源samba+mount
    Linux系统基本设置
    python实例31[urllib.request.urlopen获取股票信息]
    iptables 基本命令使用举例
    API控制VM虚拟机(VM Workstation or VM Server)
    vbs实现unicode和ascii的转化
    python语法31[string的print和format]
    Windows下运行XServer
    Perl IDE之Perl Express和Eclipse+EPIC+PadWalker
  • 原文地址:https://www.cnblogs.com/YYkun/p/15380859.html
Copyright © 2011-2022 走看看