Verification code algorithm verification for ID card verification

Source: Internet
Author: User
Import java. util. calendar; import java. util. secret; public class ValidateID {public static void main (String [] args) {ValidateID vid = new ValidateID (); System. out. println ("Enter the ID number:"); then SC = new then (System. in); String id = SC. nextLine (); vid. validateLastNum (id);}/** this algorithm can basically filter all non-conforming id cards. ** the algorithm for the last digit verification code of the id card number is as follows: SIGMA (a [I] * W [I]) mod 11 (I = 2, 3 ,..., 18) "*": * indicates the number of each digit of the ID card number, from right to left, 18 at the left, and The right side is 1. A [I]: indicates the ID number on the I-digit number W [I]: indicates the weight on the * I-digit W [I] = 2 ^ (I-1) mod 11: R = Σ (a [I] * W [I]) mod 11 (I = 2, 3 ,..., * 18) C = ID card number verification code, the relationship between R and C is as follows: R: 0 1 2 3 4 5 6 7 8 9 10 C: 1 0X9 8 7*6 5 4 3 2 from this we can see that X is 10, and 10 of the Roman numerals is X. Therefore, the new standard ID card number may contain non-numeric letters X. */Public boolean validateLastNum (String strId) {// determines whether the ID card is 18-bit if (strId. length ()! = 18) {return false;} // String is converted to StringBuffer to use the reverse () method StringBuffer sb = new StringBuffer (strId); // String reverse sb. reverse (); // returns to facilitate the operation of String id = new String (sb); // if it contains non-numbers, return false directly to exit try {Long. parseLong (id);} catch (Exception e) {// System. out. println ("this string can only be a number"); return false;} // The following algorithm follows the formula int sum = 0; int C = 0; int [] W = new int [17]; int [] a = new int [17]; for (int I = 2; I <19; I ++) {W [I-2] = (Int) (Math. pow (2, I-1) % 11); a [I-2] = Integer. parseInt (id. substring (I-1, I); sum + = (a [I-2] * W [I-2]);} int R = sum % 11; // according to the R: C correspondence, convert if (R = 0) {C = 1;} if (R = 1) {C = 0 ;}if (R = 2) {C = 10 ;}if (R> 2) {C = 12-R ;}// intercept the first character, that is, the last character String lastChar = id before conversion. substring (0, 1); // converts the truncated characters into numbers int lastNum = Integer. parseInt (lastChar); // compare the last character of the ID card with the calculated data. if the characters are equal, the result is correct. if the characters are incorrect (LastNum! = C) {// Since R = 2 corresponds to C = 10, 10 represents X in the ID card, therefore, you can determine if (C = 10 & lastChar. equals ("X") {// System. out. println ("ID card verification code"); return true;} // System. out. println ("ID card verification failed"); return false;} // System. out. println ("ID card verification code"); return true ;}}

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.