C # implementation 18 identity format verification algorithm

xiaoxiao2021-03-06  92

http://blog.mvpcn.net/lion/Articles/1401.aspx

The 18 identity card standards made a clear provision in GB11643-1999 "Citizen ID" implemented on July 1, 1999. GB11643-1999 "Citizenship Number" is the revision of the "Social Security Number", which points to the original standard name "Social Security Number" is renamed "Citizenship Number", and the GB11643-1999 "Citizen ID Number" Instead of GB11643-1989 from the date of implementation. The main contents of the "Citizen ID Number" are as follows:

First, range

This standard specifies the coding object, number of citizen numbers, the structure and performance form, so that each encoding object obtains a unique, unchanged legal number.

Second, the encoding object

The coded object of the citizenship number is a citizen with nationality of the People's Republic of China.

Third, the structure and representation of the number

1, the structure of the number

The citizen identity number is a characteristic group code, consisting of a seventeen digital body code and a check code. Arrange the order from left to right as: six digit address code, eight digit born date code, three digital sequence code and one digital check code.

2, address code

Indicates the administrative division code of the county (city, flag, district) in the county (city, flag, district) of the coding object, and is implemented in accordance with the provisions of GB / T2260.

3, birth date code

Indicates the year, month, day, pressing the GB / T7408 in accordance with the born, monthly, month, and the date, the date, month, and date.

4, order code

Indicates the sequence number of the sequence number, the sequence code, the odd number of the sequence code, the odd number of sequence code, the odd number of sequence codes, the odd number of sequence codes to the male, or even assigned to women.

5, check code

(1) Seventeen digital body code weighted summation formula

S = SUM (AI * Wi), i = 0, ..., 16, first for the top 17 digits

AI: Indicates the ID number number value in the first location

Wi: Indicates the weighting factor at the first location

Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

(2) Calculation model Y = mod (s, 11)

(3) Get the corresponding check code y: 0 1 2 3 4 5 6 7 8 9 10 Check code: 1 0 x 9 8 7 6 5 4 3 2, for example: Beijing Chaoyang District: 11010519491231002X Shantou City, Guangdong Province: 440524188001010014

The following is the program code: private string checkcidinfo (string CID) {string [] ACITY = new string [] {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "Beijing", " Tianjin "," Hebei "," Shanxi "," Inner Mongolia ", NULL, NULL, NULL, NULL, NULL," Liaoning "," Jilin "," Heilongjiang ", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL "Shanghai", "Jiangsu", "Zhejiang", "Fujian", "Jiangxi", "Shandong", NULL, NULL, NULL, "Henan", "Hubei", "Hunan", "Guangdong" "," Guangxi "," Hainan ", NULL, NULL, NULL," Chongqing "," Sichuan "," Guizhou "," Yunnan "," Tibet ", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL," Shaanxi "," Gansu "," Qinghai "," Ningxia "," Xinjiang ", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "Hong Kong", "Macau", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "Foreign"}; string info = ""; system.text.regularexpressions.Regex rg = New System.Text.RegularExpressions.Regex (@ "^ / d {17} (/ d | x) $"); system.Text.RegularExpressions.match mc = rg.match (CID); if (! Mc.success) { Return "";} CID = Cid.tolower (); CID = Cid.Replace ("x", "a"); if (ACITY [INT.PARSE (CID.SUBSTRING (0, 2))] == NULL) {Return "illegal region";} try {datetime.parse (Cid.Substring (6, 4) "-" Cid.Substring (10, 2) "-" Cid.SUBS TRING (12, 2));} catch {return "illegal birthday";} for (int i = 17; i> = 0; I -) {isum = (System.math.Pow (2, i)% 11) * int.parse (CID [17-I] .tostring (), system.globalization.numberStyles.HexNumber);

} IF (isum% 11! = 1) Return ("illegal certificate number"); return (cid.substring (0,2))] "," Cid.Substring (6, 4) "-" Cid.Substring (10, 2) "-" Cid.Substring (12, 2) "," (int.parse (cid.substring (16, 1))% 2 == 1 ¡"Men": "Female"));} Call Test: Response.Write (CHECKCIDINFO ("340524198002300019)); Response.write (" 34052419800101001x "); 15-bit authentication can be upgraded to 18 The position is verified, because it has been very busy, there is no 15- "18 method to disclose the rules:

According to the provisions of the citizen identity number in the National Standard GB 11643-1999 in the People's Republic of China, the citizen identity number is the characteristic group code, consists of the seventeenth digital body code and a digital check code. Arrange the order from left to right as: six digit address code, eight digit born date code, three digital sequence code and one digital check code.

The address code represents the administrative region of the county (city, flag, district) in the county (city, flag, district) of the coding object. The birthday period indicates the year, month, day of the birthday, month, and the year, the year, the year, month, and day without separator. The sequence code indicates the sequence number of the person born in the same year, month, and day. The odd number of sequence codes is given to men, even giving women. The check code is based on the seventeenth digit of the previous seventeen digits, calculated by the ISO 7064: 1983.MOD 11-2 check code. The calculation method will be described below.

15 identity card coding first expanded the birth year to 4 digits, simple is to increase a 19, but this is not used for people born in 1900 (such birthday is not much)

A male citizenship number is 34052419800101001, first, according to formula (1):

Σ (AI × Wi) (MOD 11) ........................................ (1)

Equation (1): i ---- Indicates the number character from the zip number, including the calibration code; AI ---- represents the number character value at the first position; Wi ---- show The weighting factor at the i position is calculated based on the formula Wi = 2 (N-1) (MOD 11).

I 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Ai 3 4 0 5 2 4 1 9 8 0 0 1 0 1 0 0 1 A1

Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1

Ai × Wi 21 36 0 25 16 16 2 9 48 0 0 9 0 5 0 0 2 A1

Calculated according to equation (1):

Σ (AI × Wi) = (21 36 0 25 16 16 2 9 48 5 0 0 2) = 189

189 ÷ 11 = 17 2/11

Σ (AI × Wi) (MOD 11) = 2

The corresponding check code is then isolated from the following table according to the results of the calculation, where x represents the calculation result 10: σ (Ai × Wi) (MOD 11) 0 1 2 3 4 5 6 7 8 9 10 Check Code character value AI 1 0 x 9 8 7 6 5 4 3 2 According to the table, the check code of the calculation result is 2, the citizenship number of the person should be 34052419800101001x.

转载请注明原文地址:https://www.9cbs.com/read-103676.html

New Post(0)