Java Authentication ID Number

Source: Internet
Author: User

The


/**
* Citizenship number is a feature combination code that consists of a 17-bit digital body code and a one-digit check code. The order of the permutations is from left to right:
* Six-digit address code, eight-digit birth date code, three-digit sequence code, and one digit check code.
* 1, Address code: Indicates the Code object resident account in the county (city, Flag, district) of the administrative codes, according to the provisions of GB/T 2260 implementation.
* 2, date of birth code: Indicates the year, month and day of the birth of the encoded object, according to the provisions of * GB/T 7408 implementation. There is no separator between the year, month, and day code.
* Example: the date of birth of a person is October 26, 1966, and the Birth date code is 19661026.
* 3, Sequence code: Indicates in the same address code identified by the area,
* For the same year, the same month, the same day birth of the number of people born, sequential code of the odd assigned to men, I thousands of assigned to women.
* 4, check code: check code using ISO 7064:1983,mod 11-2 check code system.
* (1) 17-bit digital body Code weighted summation Formula
* S = Sum (Ai * Wi), i = * 0, ..., 16, first 17-digit right sum
* Ai: Represents the number value of the ID number on the position of the first place
* Wi: Indicates Weighted factor at position I
* Wi:7 9 5 8 4 2 1 6 3 7 9 5 8 4 2 1
* (2) calculate modulo Y = mod (S, one)
* (3) get the corresponding check code by modulo
* y:0 1 2 3 4 5 6 7 8 9
* Check code: 1 0 X 9 8 7 6 5 4 3 2
*/
public class Idcard {
//weighted factor
private static final I nt[] weight = new int[] {7, 9, 5, 8, 4, 2, 1, 6,
3, 7, 9, ten, 5, 8, 4, 2, 1};
Check code
private static final int[] Checkdigit = new int[] {1, 0, ' X ', 9, 8, 7, 6,
5, 4, 3, 2};

Public Idcard () {
}
/**
* Verify that the ID is in accordance with the format
* @param idcard
* @return
*/
public boolean Verify (String idcard) {
if (idcard.length () = = 15) {
Idcard = This.update2eighteen (Idcard);
}
if (Idcard.length ()! = 18) {
return false;
}
Get the last one on the input ID, it's a check code
String checkdigit = idcard.substring (17, 18);
Compare the obtained checksum with the checksum generated by this method is equal
if (Checkdigit.equals (This.getcheckdigit (Idcard))) {
return true;
}
return false;
}

/**
* Calculate the check code of 18-digit ID card
* @param eighteenCardID18-ID Card
* @return
*/
private string Getcheckdigit (string eighteencardid) {
int remaining = 0;
if (eighteencardid.length () = = 18) {
Eighteencardid = eighteencardid.substring (0, 17);
}

if (eighteencardid.length () = = 17) {
int sum = 0;
Int[] A = new int[17];
The first 17 digits of the right sum
for (int i = 0; i <; i++) {
String k = eighteencardid.substring (i, i + 1);
A[i] = Integer.parseint (k);
}
for (int i = 0; i <; i++) {
sum = sum + weight[i] * A[i];
}
Again with 11 modulo
remaining = sum% 11;
}
return remaining = = 2? "X": string.valueof (checkdigit[remaining]);
}

/**
* Upgrade 15-digit ID card to 18-digit ID number
* @param fifteencardid
* @return
*/
private string Update2eighteen (string fifteencardid) {
The year on the 15-digit ID card has no 19, plus
String eighteencardid = fifteencardid.substring (0, 6);
Eighteencardid = Eighteencardid + "19";
Eighteencardid = Eighteencardid + fifteencardid.substring (6, 15);
Eighteencardid = Eighteencardid + this.getcheckdigit (eighteencardid);
return eighteencardid;
}

/**
* @param args
*/
public static void Main (string[] args) {
Idcard test = new Idcard ();
String idcardstr = "110105194912310025";
SYSTEM.OUT.PRINTLN ("id" + IDCARDSTR + "authentication qualified?") "
+ Test. Verify (IDCARDSTR));
Idcardstr = "440524188001010014";
SYSTEM.OUT.PRINTLN ("id" + IDCARDSTR + "authentication qualified?") "
+ Test. Verify (IDCARDSTR));
}
}

Java Authentication ID Number

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.