ID verification

ID verification

 

 

 
  
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Calendar;  
import java.util.Date;  
  
// ID verification
//Reference: http://blog.csdn.net/tanksyg/article/details/49927739
/**
 * Check the validity of ID card
 * --15-digit ID number: the 7th and 8th digits are the year of birth (two digits), the 9th and 10th digits are the month of birth, the 11th and 12th digits are the date of birth, the 15th digits are gender, and the odd number is male , even numbers are female.
 * --18-digit ID number: the 7th, 8th, 9th, and 10th digits are the year of birth (four digits), the 11th and 12th digits are the birth month, the 13th and 14th digits represent the date of birth, and the 17th digit represents the birth date. Gender, odd numbers are male, even numbers are female.
 * --The last digit is the check digit
 */  
public class IdcardValidator {  
  
    /**
     * Province and municipality code table:
     * 11 : Beijing 12 : Tianjin 13 : Hebei 14 : Shanxi 15 : Inner Mongolia   
     * 21 : Liaoning 22 : Jilin 23 : Heilongjiang 31 : Shanghai 32 : Jiangsu   
     * 33 : Zhejiang 34 : Anhui 35 : Fujian 36 : Jiangxi 37 : Shandong   
     * 41 : Henan 42 : Hubei 43 : Hunan 44 : Guangdong 45 : Guangxi 46 : Hainan   
     * 50 : Chongqing 51 : Sichuan 52 : Guizhou 53 : Yunnan 54 : Tibet   
     * 61 : Shaanxi 62 : Gansu 63 : Qinghai 64 : Ningxia 65 : Xinjiang   
     * 71 : Taiwan   
     * 81 : Hong Kong 82 : Macau   
     * 91 : Abroad
     */  
    private static String cityCode[] = { "11", "12", "13", "14", "15", "21",  
            "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42",  
            "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62",  
            "63", "64", "65", "71", "81", "82", "91" };  
  
    /**
     * Weighting factor per bit
     */  
    private static int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };  
  
    /**
     * Verify the legitimacy of all ID cards
     * @param idcard ID card
     * @return legal return true, otherwise return false
     */  
    public static boolean isValidatedAllIdcard(String idcard) {  
        if (idcard == null || "".equals(idcard)) {  
            return false;  
        }  
        if (idcard.length() == 15) {  
            return validate15IDCard(idcard);  
        }  
        return validate18Idcard(idcard);  
    }  
  
    /**
     *  
     * Judge the legitimacy of the 18-bit ID card
     *  
     *According to the regulations on citizenship numbers in the National Standard of the People's Republic of China GB11643-1999, the citizenship number is a feature combination code consisting of a seventeen-digit body code and a one-digit check code.
     * Arrangement order from left to right is: six-digit address code, eight-digit birth date code, three-digit sequence code and one-digit check code.
     *  
     * Sequence code: Indicates the sequence number assigned to people born in the same year, same month, and same day within the area identified by the same address code. The odd number of the sequence code is assigned to males, and the even numbers are assigned to females.
     *  
     * 1. The first 1 and 2 digits indicate: the code of the province where you are located;
     * 2. The 3rd and 4th digits indicate: the code of the city;
     * 3. The 5th and 6th digits indicate: the code of the district and county where it is located;
     * 4. The 7th to 14th digits indicate: birth year, month, day;
     * 5. The 15th and 16th digits indicate: the code of the local police station;
     * 6. The 17th digit indicates gender: odd number means male, even number means female;
     * 7. The 18th digit is the verification code: some say it is a personal information code, which is generally randomly generated with the computer and used to verify the correctness of the ID card. The check code can be a number from 0 to 9, sometimes represented by x.
     *  
     * The calculation method of the eighteenth digit (check code) is: 1. Multiply the 17 digits of the previous ID number by different coefficients. The coefficients from the first to the seventeenth are: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
     *  
     * 2. Add the result of multiplying these 17-digit numbers by the coefficients.
     *  
     * 3. Add and divide by 11 to see what the remainder is
     *  
     * 4. The remainder can only have 11 numbers 0 1 2 3 4 5 6 7 8 9 10. The corresponding last ID number is 1 0 X 9 8 7 6 5 4 3
     * 2。
     *  
     * 5. It is known from the above that if the remainder is 2, the Roman numeral X will appear on the 18th digit of the ID card. If the remainder is 10, the last digit of the ID card is 2.
     *  
     *  
     * @param idcard
     * @return
     */  
    public static boolean validate18Idcard(String idcard) {  
        if (idcard == null) {  
            return false;  
        }  
  
        // non-18 bits are false  
        if (idcard.length() != 18) {  
            return false;  
        }  
        // get the first 17 digits  
        String idcard17 = idcard.substring(0, 17);  
  
        // The first 17 digits are all numbers  
        if (!isDigital(idcard17)) {  
            return false;  
        }  
  
        String provinceid = idcard.substring(0, 2);  
        // check province  
        if (!checkProvinceid(provinceid)) {  
            return false;  
        }  
  
        // check date of birth  
        String birthday = idcard.substring(6, 14);  
  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
  
        try {  
            Date birthDate = sdf.parse(birthday);  
            String tmpDate = sdf.format(birthDate);  
            if (!tmpDate.equals(birthday)) {// The date of birth is incorrect  
                return false;  
            }  
  
        } catch (ParseException e1) {  
  
            return false;  
        }  
  
        // get the 18th bit  
        String idcard18Code = idcard.substring(17, 18);  
  
        char c[] = idcard17.toCharArray();  
  
        int bit[] = converCharToInt(c);  
  
        int sum17 = 0;  
  
        sum17 = getPowerSum(bit);  
  
        // Take the sum value and modulo 11 to get the remainder to judge the check code  
        String checkCode = getCheckCodeBySum(sum17);  
        if (null == checkCode) {  
            return false;  
        }  
        // Match the 18th digit of the ID card with the calculated school code, if not equal, it is false  
        if (!idcard18Code.equalsIgnoreCase(checkCode)) {  
            return false;  
        }  
  
        return true;  
    }  
  
    /**
     * Verify 15-digit ID card
     *  
     * <pre>
     * Only check province and date of birth
     * </pre>
     *  
     * @param idcard
     * @return
     */  
    public static boolean validate15IDCard(String idcard) {  
        if (idcard == null) {  
            return false;  
        }  
        // non-15 bits are false  
        if (idcard.length() != 15) {  
            return false;  
        }  
  
        // 15 is all numbers  
        if (!isDigital(idcard)) {  
            return false;  
        }  
  
        String provinceid = idcard.substring(0, 2);  
        // check province  
        if (!checkProvinceid(provinceid)) {  
            return false;  
        }  
  
        String birthday = idcard.substring(6, 12);  
  
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");  
  
        try {  
            Date birthDate = sdf.parse(birthday);  
            String tmpDate = sdf.format(birthDate);  
            if (!tmpDate.equals(birthday)) {// ID card date is wrong  
                return false;  
            }  
  
        } catch (ParseException e1) {  
  
            return false;  
        }  
  
        return true;  
    }  
  
    /**
     * Convert 15-digit ID card to 18-digit ID card
     *  
     * @param idcard
     * @return
     */  
    public static String convertIdcarBy15bit(String idcard) {  
        if (idcard == null) {  
            return null;  
        }  
  
        // non-15-digit ID card  
        if (idcard.length() != 15) {  
            return null;  
        }  
  
        // 15 is all numbers  
        if (!isDigital(idcard)) {  
            return null;  
        }  
  
        String provinceid = idcard.substring(0, 2);  
        // check province  
        if (!checkProvinceid(provinceid)) {  
            return null;  
        }  
  
        String birthday = idcard.substring(6, 12);  
  
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");  
  
        Date birthdate = null;  
        try {  
            birthdate = sdf.parse(birthday);  
            String tmpDate = sdf.format(birthdate);  
            if (!tmpDate.equals(birthday)) {// ID card date is wrong  
                return null;  
            }  
  
        } catch (ParseException e1) {  
            return null;  
        }  
  
        Calendar cday = Calendar.getInstance();  
        cday.setTime(birthdate);  
        String year = String.valueOf(cday.get(Calendar.YEAR));  
  
        String idcard17 = idcard.substring(0, 6) + year + idcard.substring(8);  
  
        char c[] = idcard17.toCharArray();  
        String checkCode = "";  
  
        // Convert character array to integer array  
        int bit[] = converCharToInt(c);  
  
        int sum17 = 0;  
        sum17 = getPowerSum(bit);  
  
        // Get the sum value and modulo 11 to get the remainder to check the code  
        checkCode = getCheckCodeBySum(sum17);  
  
        // Can't get check digit  
        if (null == checkCode) {  
            return null;  
        }  
        // Concatenate the first 17 digits with the 18th check code  
        idcard17 += checkCode;  
        return idcard17;  
    }  
  
    /**
     * Check province
     *  
     * @param provinceid
     * @return returns TRUE legally, otherwise returns FALSE
     */  
    private static boolean checkProvinceid(String provinceid) {  
        for (String id : cityCode) {  
            if (id.equals(provinceid)) {  
                return true;  
            }  
        }  
        return false;  
    }  
  
    /**
     * Digital verification
     *  
     * @param str
     * @return
     */  
    private static boolean isDigital(String str) {  
        return str.matches("^[0-9]*$");  
    }  
  
    /**
     * After multiplying each bit of the ID card and the weighting factor of the corresponding bit, the sum value is obtained
     *  
     * @param bit
     * @return
     */  
    private static int getPowerSum(int[] bit) {  
  
        int sum = 0;  
  
        if (power.length != bit.length) {  
            return sum;  
        }  
  
        for (int i = 0; i < bit.length; i++) {  
            for (int j = 0; j < power.length; j++) {  
                if (i == j) {  
                    sum = sum + bit[i] * power[j];  
                }  
            }  
        }  
        return sum;  
    }  
  
    /**
     * Take the sum value and modulo 11 to get the remainder to judge the check code
     *  
     * @param checkCode
     * @param sum17
     * @return check digit
     */  
    private static String getCheckCodeBySum(int sum17) {  
        String checkCode = null;  
        switch (sum17 % 11) {  
        case 10:  
            checkCode = "2";  
            break;  
        case 9:  
            checkCode = "3";  
            break;  
        case 8:  
            checkCode = "4";  
            break;  
        case 7:  
            checkCode = "5";  
            break;  
        case 6:  
            checkCode = "6";  
            break;  
        case 5:  
            checkCode = "7";  
            break;  
        case 4:  
            checkCode = "8";  
            break;  
        case 3:  
            checkCode = "9";  
            break;  
        case 2:  
            checkCode = "x";  
            break;  
        case 1:  
            checkCode = "0";  
            break;  
        case 0:  
            checkCode = "1";  
            break;  
        }  
        return checkCode;  
    }  
  
    /**
     * Convert character array to integer array
     *  
     * @param c
     * @return
     * @throws NumberFormatException
     */  
    private static int[] converCharToInt(char[] c) throws NumberFormatException {  
        int[] a = new int[c.length];  
        int k = 0;  
        for (char temp : c) {  
            a[k++] = Integer.parseInt(String.valueOf(temp));  
        }  
        return a;  
    }  
  
    public static void main(String[] args) throws Exception {  
        String idcard15 = "130321860311123";  
        String idcard18 = "13022919830501231";
        // 15 digit ID  
        System.out.println(isValidatedAllIdcard(idcard15));  
        // 18-bit ID  
        System.out.println(isValidatedAllIdcard(idcard18));  
        // 15-bit ID card to 18-bit ID card  
        System.out.println(convertIdcarBy15bit(idcard15));  
    }  
}  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Donate to developers 

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (support Alipay, WeChat, and the buckle group), if you have no money to support a personal field, thank you.

 

Personal homepage : http://knight-black-bob.iteye.com/



 
 
 Thank you for your sponsorship, I will do better!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326996928&siteId=291194637