This article is a mirror article of machine translation, please click here to jump to the original article.

View: 23575|Reply: 0

[Source] C# hides the middle digit of the ID number

[Copy link]
Posted on 1/8/2016 2:43:31 PM | | | |


As shown in the picture above, it is the result of the ID card being hidden, and the middle digits are hidden as * signs.

When we usually display the ID card to the user on the web page, it usually cannot be displayed to the user to prevent others from stealing the user's information after maliciously logging in

Here's how:

  1. /// <summary>
  2.         /// 隐藏身份证号码
  3.         /// </summary>
  4.         /// <param name="card"></param>
  5.         /// <returns></returns>
  6.         public static string IDCardHide(string card)
  7.         {
  8.             string temp = null;
  9.             if (card.Length == 18)
  10.             {
  11.                 temp = card.Substring(0, 5) + "***********" + card.Substring(16, 2);
  12.             }
  13.             if (card.Length == 15)
  14.             {
  15.                 temp = card.Substring(0, 4) + "********" + card.Substring(12, 3);
  16.             }
  17.             return temp;
  18.         }
  19.         /// <summary>  
  20.         /// 验证身份证合理性  
  21.         /// </summary>  
  22.         /// <param name="Id"></param>  
  23.         /// <returns></returns>  
  24.         public static bool CheckIDCard(string idNumber)
  25.         {
  26.             if (idNumber.Length == 18)
  27.             {
  28.                 bool check = CheckIDCard18(idNumber);
  29.                 return check;
  30.             }
  31.             else if (idNumber.Length == 15)
  32.             {
  33.                 bool check = CheckIDCard15(idNumber);
  34.                 return check;
  35.             }
  36.             else
  37.             {
  38.                 return false;
  39.             }
  40.         }
  41.         /// <summary>  
  42.         /// 18位身份证号码验证  
  43.         /// </summary>  
  44.         private static bool CheckIDCard18(string idNumber)
  45.         {
  46.             long n = 0;
  47.             if (long.TryParse(idNumber.Remove(17), out n) == false
  48.                 || n < Math.Pow(10, 16) || long.TryParse(idNumber.Replace('x', '0').Replace('X', '0'), out n) == false)
  49.             {
  50.                 return false;//数字验证  
  51.             }
  52.             string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  53.             if (address.IndexOf(idNumber.Remove(2)) == -1)
  54.             {
  55.                 return false;//省份验证  
  56.             }
  57.             string birth = idNumber.Substring(6, 8).Insert(6, "-").Insert(4, "-");
  58.             DateTime time = new DateTime();
  59.             if (DateTime.TryParse(birth, out time) == false)
  60.             {
  61.                 return false;//生日验证  
  62.             }
  63.             string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
  64.             string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
  65.             char[] Ai = idNumber.Remove(17).ToCharArray();
  66.             int sum = 0;
  67.             for (int i = 0; i < 17; i++)
  68.             {
  69.                 sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
  70.             }
  71.             int y = -1;
  72.             Math.DivRem(sum, 11, out y);
  73.             if (arrVarifyCode[y] != idNumber.Substring(17, 1).ToLower())
  74.             {
  75.                 return false;//校验码验证  
  76.             }
  77.             return true;//符合GB11643-1999标准  
  78.         }


  79.         /// <summary>  
  80.         /// 16位身份证号码验证  
  81.         /// </summary>  
  82.         private static bool CheckIDCard15(string idNumber)
  83.         {
  84.             long n = 0;
  85.             if (long.TryParse(idNumber, out n) == false || n < Math.Pow(10, 14))
  86.             {
  87.                 return false;//数字验证  
  88.             }
  89.             string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  90.             if (address.IndexOf(idNumber.Remove(2)) == -1)
  91.             {
  92.                 return false;//省份验证  
  93.             }
  94.             string birth = idNumber.Substring(6, 6).Insert(4, "-").Insert(2, "-");
  95.             DateTime time = new DateTime();
  96.             if (DateTime.TryParse(birth, out time) == false)
  97.             {
  98.                 return false;//生日验证  
  99.             }
  100.             return true;
  101.         }
Copy code






Previous:asp.net Escape of the Aite symbol in MVC
Next:sql SELECT
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com