Java-based 18-bit ID card format Verification Algorithm

Source: Internet
Author: User

A citizenship number is a combination of features and consists of a 17-digit ontology code and a digital verification code. the six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code are arranged from left to right.
1. The address code indicates the administrative division code of the county (city, flag, district) where the resident account of the encoding object is located, according to GB/t2260.

2. The date of birth indicates the year, month, and day of the birth of the Code object, which must be implemented in accordance with GB/t7408. There is no separator between the year, month, and day codes. For example, if a person is born on January 1, October 26, 1966, the birth date code is 19661026.

3. The sequence code indicates the sequence number of the person born on the same year, the same month, and the same day within the region specified by the same address code. The odd number of the sequence code is allocated to the man, thousands of even numbers are allocated to female.

4. The Verification Code adopts iso7064: 1983, MOD11-2 verification code system.

1) The weighted sum formula S = sum (ai * WI), I = 0 ,..., 16. First, sum the weights of the first 17 digits. AI: ID card number at the I position. Digital value: wi: the weighting factor at the I position. Wi: 7910584216379105842

2) computing model Y = Mod (S, 11)

3) Get the corresponding verification code through the model: 012345678910 verification code: 10x98765432

The following code is implemented in Java:

/** Idcard. Java created on 2004-11-517: 03: 37 **/

Package org. yz21.study. idcard;

/**
* @ Author violin 2004-11-517: 03: 37
* Copyright www. yz21.org2003-2004.
*/
Public class idcard {
// Wi = 2 (n-1) (mod11)
Final int [] Wi = };

// Verify Digit
Final int [] Vi = {, X, 9, 8, 7, 6, 5, 4, 3, 2 };

Private int [] Ai = new int [18];

Public idcard (){}

// Verify
Public Boolean verify (string idcard ){
If (idcard. Length () = 15 ){
Idcard = uptoeighteen (idcard );
}
If (idcard. Length ()! = 18 ){
Return false;
}
String verify = idcard. substring (17,18 );
If (verify. Equals (getverify (idcard ))){
Returntrue;
}
Returnfalse;
}

// Get verify
Public String getverify (string eightcardid ){
Int remaining = 0;

If (eightcardid. Length () = 18 ){
Eightcardid = eightcardid. substring (0, 17 );
}

If (eightcardid. Length () = 17 ){
INTSUM = 0;
For (INTI = 0; I <17; I ++ ){
String K = eightcardid. substring (I, I + 1 );
Ai = integer. parseint (k );
}

For (INTI = 0; I <17; I ++ ){
Sum = sum + wi * Ai;
}
Remaining = sum % 11;
}
Return remaining = 2? "X": String. valueof (VI [remaining]);
}

// 15 update to 18
Public String uptoeighteen (string fifteencardid ){
String eightcardid = teteencardid. substring );
Eightcardid = eightcardid + "19 ";
Eightcardid = eightcardid + fifteencardid. substring (6, 15 );
Eightcardid = eightcardid + getverify (eightcardid );
Return eightcardid;
}

}

Test code: the unit test tool used is JUnit.

/** Idcardtest. Java created on 2004-11-517: 32: 12 **/

Package org. yz21.study. idcard;

Import JUnit. Framework. test;
Import JUnit. Framework. testcase;
Import JUnit. Framework. testsuite;

/**
* @ Author violin 2004-11-517: 32: 12
* Copyright www. yz21.org2003-2004.
*/
Public class idcardtest extends testcase {

Private string idcard1 = "11010519491231002x ";
Private string idcard2 = "440524188001010014 ";

Public void testverify (){
Idcardidcard = newidcard ();
This. asserttrue (idcard. Verify (idcard1 ));
This. asserttrue (idcard. Verify (idcard2 ));
}

Public static testsuite (){
Return new testsuite (idcardtest. Class );
}

Public static void main (string [] ARGs ){
JUnit. textui. testrunner. Run (Suite ());
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.