Due to project needs, I wrote a C# method for ID card verification based on online information. This method was written under VS2005[C/S].
/*
*Theoretical part:
* 15-digit ID number = 6-digit area code + 6-digit birthday + 3-digit serial number
* 18-digit ID number = 6-digit area code + 8-digit birthday + 3-digit serial number + 1-digit verification code
*
*The first two digits of the country code for each province, city, and region are:
* Beijing 11 Jilin 22 Fujian 35 Guangdong 44 Yunnan 53 Tianjin 12 Heilongjiang 23 Jiangxi 36 Guangxi 45 Tibet 54 Hebei 13 Shanghai 31 Shandong 37 Hainan 46 Shaanxi 61 Shanxi 14 Jiangsu 32 Henan 41 Chongqing 50
Gansu 62 Inner Mongolia 15 Zhejiang 33 Hubei 42 Sichuan 51 Qinghai 63 Liaoning 21 Anhui 34 Hunan 43 Guizhou 52 Ningxia 64 Xinjiang 65 Taiwan 71 Hong Kong 81 Macau 82 Overseas 91
*The 18-digit ID card standard is clearly stipulated in GB11643-1999 "Citizen Identity Number" implemented by the State Administration of Quality and Technical Supervision on July 1, 1999.
*GB11643-1999 "Citizen Identity Number" is a revised version of GB11643-1989 "Social Security Number", which states that the original standard name "Social Security Number" will be renamed "Citizen Identity Number". In addition, GB11643-1999 "Citizen Identity Number" It will replace GB11643-1989 from the date of implementation.
*The citizen identity number is a characteristic combination code, consisting of a seventeen-digit body code and a one-digit check code. The order from left to right is: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code. Its meaning is as follows:
*1. Address code: indicates the administrative division code of the county (city, banner, district) where the coding object’s permanent residence is located, and is implemented in accordance with the provisions of GB/T2260.
*2. Date of birth code: indicates the year, month, and day of the birth of the encoding object. It is implemented in accordance with the provisions of GB/T7408. The year, month, and day are represented by 4 digits, 2 digits, and 2 digits respectively, without separators between them.
*3. Sequence code: indicates the sequence number assigned to people born in the same year, same month, and same day within the area identified by the same address code. Odd numbers of the sequence code are assigned to males, and even numbers are assigned to females.
*Calculation method of verification:
*1. Weighted sum of the first 17 digits of the ontology code
*The formula is: S = Sum(Ai * Wi), i = 0, ... , 16
*where Ai represents the digital value of the ID card number at the i-th position, Wi represents the weighting factor at the i-th position, and the corresponding values are:
*7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
*2. Take the calculation result modulo 11
*Y = mod(S, 11)
*3. The corresponding relationship between the check codes obtained based on the module value is:
*Y value: 0 1 2 3 4 5 6 7 8 9 10
*Check code: 1 0 X 9 8 7 6 5 4 3 2
*/
code
1 /// <summary>
2 /// ID card verification
3 /// </summary>
4 /// <param name="Id">ID card number</param>
5 /// <returns></returns>
6 public bool CheckIDCard(string Id)
7 {
8 if (Id.Length == 18)
9 {
10 bool check = CheckIDCard18(Id);
11 return check;
12}
13 else if (Id.Length == 15)
14 {
15 bool check = CheckIDCard15(Id);
16 return check;
17}
18 else
19 {
20 return false;
twenty one }
twenty two }
23 /// <summary>
24 /// 18-digit ID card verification
25 /// </summary>
26 /// <param name="Id">ID card number</param>
27 /// <returns></returns>
28 private bool CheckIDCard18(string Id)
29 {
30 long n = 0;
31 if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0') .Replace('X', '0'), out n) == false)
32 {
33 return false;//Number verification
34}
35 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
36 if (address.IndexOf(Id.Remove(2)) == -1)
37 {
38 return false;//Province verification
39 }
40 string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
41 DateTime time = new DateTime();
42 if (DateTime.TryParse(birth, out time) == false)
43 {
44 return false;//Birthday verification
45 }
46 string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
47 string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',') ;
48 char[] Ai = Id.Remove(17).ToCharArray();
49 int sum = 0;
50 for (int i = 0; i < 17; i++)
51 {
52 sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
53}
54 int y = -1;
55 Math.DivRem(sum, 11, out y);
56 if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
57 {
58 return false;//Verification code verification
59 }
60 return true; //Comply with GB11643-1999 standard
61 }
62 /// <summary>
63 /// 15-digit ID card verification
64 /// </summary>
65 /// <param name="Id">ID card number</param>
66 /// <returns></returns>
67 private bool CheckIDCard15(string Id)
68 {
69 long n = 0;
70 if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
71 {
72 return false;//Number verification
73}
74 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
75 if (address.IndexOf(Id.Remove(2)) == -1)
76 {
77 return false;//Province verification
78 }
79 string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
80 DateTime time = new DateTime();
81 if (DateTime.TryParse(birth, out time) == false)
82 {
83 return false;//Birthday verification
84}
85 return true;//Conforms to the 15-digit ID card standard