Este artículo es un artículo espejo de traducción automática, por favor haga clic aquí para saltar al artículo original.

Vista: 23575|Respuesta: 0

[Fuente] C# oculta el dígito central del número ID

[Copiar enlace]
Publicado en 8/1/2016 14:43:31 | | | |


Como se muestra en la imagen anterior, es el resultado de que la tarjeta de identificación está oculta, y los dígitos centrales están ocultos como signos *.

Cuando normalmente mostramos la tarjeta de identificación al usuario en la página web, normalmente no puede mostrarse para evitar que otros roben la información del usuario tras iniciar sesión de forma maliciosa

Así es como:

  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.         }
Copiar código






Anterior:asp.net Escape del símbolo de Aite en MVC
Próximo:sql SELECT
Renuncia:
Todo el software, materiales de programación o artículos publicados por Code Farmer Network son únicamente para fines de aprendizaje e investigación; El contenido anterior no se utilizará con fines comerciales o ilegales; de lo contrario, los usuarios asumirán todas las consecuencias. La información de este sitio proviene de Internet, y las disputas de derechos de autor no tienen nada que ver con este sitio. Debes eliminar completamente el contenido anterior de tu ordenador en un plazo de 24 horas desde la descarga. Si te gusta el programa, por favor apoya el software genuino, compra el registro y obtén mejores servicios genuinos. Si hay alguna infracción, por favor contáctanos por correo electrónico.

Mail To:help@itsvse.com