Second generation id card verification example

  • 2020-04-01 03:03:59
  • OfStack

(1) structure of 18 id card number

The citizenship number is the characteristic combination code, which is composed of 17 digit main body codes and one digit check codes.

The order is from left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code.
1. Address code
Represents the code of administrative division of the county (city, flag, district) where the coding object's permanent residence is located, which is executed according to the provisions of GB/T2260.
2. Date of birth code
Represents the year, month and date of the birth of the encoding object, which is executed according to the provisions of GB/T7408, and there is no separator between the year, month and day codes.
3. Sequence code
Refers to the sequence number assigned to persons born in the same year, the same month or the same day within the area marked by the same address code. The odd number of the sequence code is assigned to males and the even number to females.
4. Calculation steps of check code

(1) weighted summation formula of 17 digit main body codes
S = Sum(Ai * Wi), I = 0... So let's take the sum of the weights of the first 17 digits
Ai: represents the numeric value of the id card number on position I (0~9)
Wi:7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 (the weighting factor at position I)
(2) the calculation model
Y = mod (11) S,

(3) according to the module, find 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

(2) according to the 17 digit ontology code to obtain the last check code program example


public class Id18 {
    int[] weight={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};    //Seventeen digit body weight
    char[] validate={ '1','0','X','9','8','7','6','5','4','3','2'};    //Mod11, corresponding check code character value & PI;    

    public char getValidateCode(String id17){
        int sum=0;
        int mode=0;
        for(int i=0;i<id17.length();i++){
            sum=sum+Integer.parseInt(String.valueOf(id17.charAt(i)))*weight[i];
        }
        mode=sum%11;
        return validate[mode];
    }

    public static void main(String[] args){
        Id18 test=new Id18();
        System.out.println(" Id card verification code: "+test.getValidateCode("14230219700101101"));    //Check code of this id card: 3
    }
}

(3) explanation

1. The program can obtain the corresponding verification code according to the existing 17-digit ontology code.

2. The program can remove the identity card number with incorrect verification code.

3.15 the birth year of the id card is 2 digits after the year, without the last 1 digit check code.

4. A complete id card has 18 digits, and the last checksum may be non-digits. In one of our projects, the database stores the first 17 digits so that some SQL statements (such as inner join) can be accelerated!!


Related articles: