Java-based 18-bit ID card format Verification Algorithm

Source: Internet
Author: User
Java-based 18-bit ID card format verification algorithm-general Linux technology-Linux programming and kernel information. The following is a detailed description. 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. Address Code
Indicates the Administrative Code of the county (city, flag, district) where the resident account of the encoding object is located, which is executed according to GB/T 2260.


2. Date of birth
Indicates the year, month, and day when the encoding object is born. The code is executed according to GB/T 7408. 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. 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 men, and the even number is allocated to women.


4. Verification Code
The verification code uses the ISO 7064: 1983, MOD 11-2 Verification code system.


(1) 17-digit ontology weighted sum formula
S = Sum (Ai * Wi), I = 0,..., 16, first Sum the right of the first 17 digits
Ai: indicates the number of ID card numbers at location I.
Wi: indicates the weighting factor at position I.
Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2


(2) Computing Model
Y = mod (S, 11)


(3) obtain the corresponding verification code through the modulo
Y: 0 1 2 3 4 5 6 7 8 9 10
Verification Code: 1 0X9 8 7 6 5 4 3 2

The following code is implemented in java:


/*
* IDCard. java Created on 17:03:37
*
*/
Package org. yz21.study. idcard;


/**
* @ Author violin 17:03:37
* Copyright www.yz21.org 2003-2004
*/
Public class IDCard {
// Wi = 2 (n-1) (mod 11)
Final int [] wi = };


// Verify digit
Final int [] vi = {, 'x', 9, 8, 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 ))){
Return true;
}
Return false;
}


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


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


If (eightcardid. length () = 17 ){
Int sum = 0;
For (int I = 0; I <17; I ++ ){
String k = eightcardid. substring (I, I + 1 );
Ai = Integer. parseInt (k );
}


For (int I = 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 17:32:12
*
*/
Package org. yz21.study. idcard;


Import junit. framework. Test;
Import junit. framework. TestCase;
Import junit. framework. TestSuite;


/**
* @ Author violin 17:32:12
* Copyright www.yz21.org 2003-2004
*/
Public class IDCardTest extends TestCase {


Private String idcard1 = "11010519491231002X ";
Private String idcard2 = "440524188001010014 ";


Public void testVerify (){
IDCard idcard = new IDCard ();
This. assertTrue (idcard. Verify (idcard1 ));
This. assertTrue (idcard. Verify (idcard2 ));
}


Public static Test suite (){
Return new TestSuite (IDCardTest. class );
}


Public static void main (String [] args ){
Junit. textui. TestRunner. run (suite ());
}
}
Related Article

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.