Java-ID card number verification

Source: Internet
Author: User

 

ID card number encoding principle: Number Structure

A citizenship number is a combination of features and consists of a 17-digit ontology code and a 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.

 

Verification code calculation

(1) 17-digit ontology weighted sum formula
S = sum (ai * WI), I = 0,..., 16, first to the front
17-digit weighted sum
AI: indicates
ID card number on I
Wi: indicates
I-position Weighting Factor
Wi: 7 9 10 5 8
4 2 1 6 3
7 9 10 5 8
4 2

S: = (4*7 + 5*9 + 2*10 + 1*5 + 2*8 + 4*4 + 1*2 + 9*1 + 8*6 + 3*3 + 0*7 + 7*9 + 0*10 + 3*5 + 0*8 + 3*4 + 9*2) =
306;

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

Y = 306% 11 = 9;

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

Java code implementation:
Class verifyidcard {// Wi = 2 (n-1) (mod 11); weighting factor final int [] Wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}; // check code final int [] Vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2}; private int [] Ai = new int [18]; Public verifyidcard () {}// the verification code 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 (this. getverify (idcard) {return true;} return false;} // 15-bit Public String uptoeighteen (string fifteen) {stringbuffer eighteen = new stringbuffer (fifteen ); eighteen = eighteen. insert (6, "19"); Return eighteen. tostring () ;}// calculate the last checksum value Public String getverify (string eighteen) {int remain = 0; If (eighteen. Length () = 18) {eighteen = eighteen. substring (0, 17);} If (eighteen. length () = 17) {int sum = 0; For (INT I = 0; I <17; I ++) {string K = eighteen. substring (I, I + 1); AI [I] = integer. valueof (k) ;}for (INT I = 0; I <17; I ++) {sum + = wi [I] * Ai [I];} remain = sum % 11;} return remain = 2? "X": String. valueof (VI [remain]);}

 

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.