身份證識別(java)

身份號碼是特徵組合碼,由十七位數字本體碼和一位校驗碼組成。

   排列順序從左至右依次爲:六位數字地址碼,八位數字出生日期碼,三位數字順序碼和一位數字校驗碼。

  身份證地址碼錶示編碼對象常住戶口所在縣(市、旗、區)的行政區劃代碼,按GB/T2260的規定執行。

  身份證出生日期碼錶示編碼對象出生的年、月、日,按GB/T7408的規定執行,年、月、日代碼之間不用分隔符。

  身份證順序碼錶示在同一地址碼所標識的區域範圍內,對同年、同月、同日出生的人編定的順序號,順序碼的奇數分配給男性,偶數分配給女性。

身份證校驗碼是十七位數字本體碼加權求和而得出的。


編碼規則:

   公民身份號碼是特徵組合碼,由十七位數字本體碼和一位校驗碼組成。排列順序從左至右依次爲:六位數字地址碼,八位數字出生日期碼,三位數字順序碼和一位校驗碼,可以用字母表示如爲ABCDEFYYYYMMDDXXXR。其含義如下:
1. 地址碼(ABCDEF):表示編碼對象常住戶口所在縣(市、旗、區)的行政區劃代碼,按GB/T2260的規定執行。

2. 出生日期碼(YYYYMMDD):表示編碼對象出生的年、月、日,按GB/T7408的規定執行,年、月、日分別用4位、2位(不足兩位加0)、2位(不足兩位加0)數字表示,之間不用分隔符。

3. 順序碼(XXX):表示在同一地址碼所標識的區域範圍內,對同年、同月、同日出生的人編定的順序號,順序碼的奇數分配給男性,偶數分配給女性。

4.校驗碼(R),一位數字,通過前17位數字根據一定計算得出,檢驗碼分別是“0、1、2、……10”共11個數字,當檢驗碼爲“10”時,爲了保證公民身份證號碼18位,所以用“X”表示。

1、關於中國居民身份證的常識:
 我國現行使用公民身份證號碼有兩種尊循兩個國家標準,〖GB 11643-1989〗和〖GB 11643-1999〗。
   〖GB 11643-1989〗中規定的是15位身份證號碼:排列順序從左至右依次爲:六位數字地址碼,六位數字出生日期碼,三位數字順序碼,其中出生日期碼不包含世紀數。

       〖GB 11643-1999〗中規定的是18位身份證號碼:公民身份號碼是特徵組合碼,由十七位數字本體碼和一位數字校驗碼組成。排列順序從左至右依次爲:六位數字地址碼,八位數字出生日期碼,三位數字順序碼和一位數字校驗碼。
地址碼:表示編碼對象常住戶口所在縣(市、旗、區)的行政區劃代碼。
出生日期碼:表示編碼對象出生的年、月、日,其中年份用四位數字表示,年、月、日之間不用分隔符。
順序碼:表示同一地址碼所標識的區域範圍內,對同年、同月、同日出生的人員編定的順序號。順序碼的奇數分給男性,偶數分給女性。
校驗碼:是根據前面十七位數字碼,按照ISO 7064:1983.MOD 11-2校驗碼計算出來的檢驗碼。

關於身份證號碼最後一位的校驗碼的算法如下:
∑(a[i]*W[i]) mod 11 ( i = 2, 3, ..., 18 )
    "*" : 表示乘號
i: 表示身份證號碼每一位的序號,從右至左,最左側爲18,最右側爲1。
a[i]: 表示身份證號碼第 i 位上的號碼
W[i]: 表示第 i 位上的權值 W[i] = 2^(i-1) mod 11
設:R = ∑(a[i]*W[i]) mod 11 ( i = 2, 3, ..., 18 )
C = 身份證號碼的校驗碼

  則R和C之間的對應關係如下表:
    R:0 1 2 3 4 5 6 7 8 9 10
    C:1 0 X 9 8 7 6 5 4 3 2

  由此看出 X 就是 10,羅馬數字中的 10 就是X,所以在新標準的身份證號碼中可能含有非數字的字母X。

算法:

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Random;
/**
 * @author 成曉旭
 *
 */
public class Identity {
    //位權值數組
    private static byte[] Wi=new byte[17];
    // 身份證前部分字符數
    private static final byte fPart = 6;
    // 身份證算法求模關鍵值
    private static final byte fMod = 11;
    //舊身份證長度
    private static final byte oldIDLen = 15;
    //新身份證長度
    private static final byte newIDLen = 18;
    //新身份證年份標誌
    private static final String yearFlag = "19";
     //校驗碼串 
    private static final String CheckCode="10X98765432"; 
     //最小的行政區劃碼
    private static final int minCode = 150000;
     // 最大的行政區劃碼
    private static final int maxCode = 700000;
     //舊身份證號碼
     //private String oldIDCard="";
     //新身份證號碼
     //private String newIDCard="";
     //地區及編碼
      //private String Area[][2] = 
    private static void setWiBuffer(){
        for(int i=0;i< Wi.length;i++){    
            int k = (int) Math.pow(2, (Wi.length-i));
            Wi[i] = (byte)(k % fMod);
        }
    }
    //獲取新身份證的最後一位:檢驗位
    private static String getCheckFlag(String idCard){
        int sum = 0;
       //進行加權求和 
        for(int i=0; i< 17; i++){        
            sum += Integer.parseInt(idCard.substring(i,i+1)) * Wi[i];
        }
        //取模運算,得到模值
        byte iCode = (byte) (sum % fMod); 
        return CheckCode.substring(iCode,iCode+1);
    }
    //判斷串長度的合法性
    private static boolean checkLength(final String idCard,boolean newIDFlag){
        boolean right = (idCard.length() == oldIDLen) || (idCard.length() == newIDLen);
        newIDFlag = false;
        if(right){
            newIDFlag = (idCard.length() == newIDLen);
        }
        return right;
    }
    //獲取時間串
    private static String getIDDate(final String idCard,boolean newIDFlag){
        String dateStr = "";
        if(newIDFlag)
            dateStr = idCard.substring(fPart,fPart+8);
        else
            dateStr = yearFlag + idCard.substring(fPart,fPart+6);
        return dateStr;
    }
    //判斷時間合法性
    private static boolean checkDate(final String dateSource){
        String dateStr = dateSource.substring(0,4)+"-"+dateSource.substring(4,6)+"-"+dateSource.substring(6,8);
        System.out.println(dateStr);
        DateFormat df = DateFormat.getDateInstance();
        df.setLenient(false);
        try {
            Date date= df.parse(dateStr);
            return (date!=null);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            return false;
        }
    }
    //舊身份證轉換成新身份證號碼
    public static String getNewIDCard(final String oldIDCard){
        //初始化方法
        Identity.setWiBuffer();
        if(!checkIDCard(oldIDCard)){
            return oldIDCard;
        }
        String newIDCard = oldIDCard.substring(0, fPart);
        newIDCard += yearFlag;
        newIDCard += oldIDCard.substring(fPart, oldIDCard.length());
        String ch = getCheckFlag(newIDCard);
        newIDCard += ch;
        return newIDCard;
    }
    //新身份證轉換成舊身份證號碼
    public static String getOldIDCard(final String newIDCard){
        //初始化方法
        Identity.setWiBuffer();
        if(!checkIDCard(newIDCard)){
            return newIDCard;
        }
        String oldIDCard = newIDCard.substring(0,fPart)+
                    newIDCard.substring(fPart+yearFlag.length(),newIDCard.length()-1);
        return oldIDCard;
    }
    //判斷身份證號碼的合法性
    public static boolean checkIDCard(final String idCard){
        //初始化方法
        Identity.setWiBuffer();
        boolean isNew = false;
        //String message = "";
        if (!checkLength(idCard,isNew)){
            //message = "ID長度異常";
            return false;
        }
        String idDate = getIDDate(idCard, isNew);
        if(!checkDate(idDate)){
            //message = "ID時間異常";
            return false;
        }
        if(isNew){
            String checkFlag = getCheckFlag(idCard);
            String theFlag = idCard.substring(idCard.length()-1,idCard.length());
            if(!checkFlag.equals(theFlag)){
                //message = "新身份證校驗位異常";
                return false;
            }
        }
        return true;
    }
    //獲取一個隨機的"僞"身份證號碼
    public static String getRandomIDCard(final boolean idNewID){
        //初始化方法
        Identity.setWiBuffer();
        Random ran = new Random();
        String idCard = getAddressCode(ran)+getRandomDate(ran,idNewID)+getIDOrder(ran);
        if(idNewID){
            String ch = getCheckFlag(idCard);
            idCard += ch;
        }
        return idCard;
    }
    //產生隨機的地區編碼
    private static String getAddressCode(Random ran) {
        if(ran==null){
            return "";
        }else{
            int addrCode = minCode + ran.nextInt(maxCode-minCode);
            return Integer.toString(addrCode);
        }
    }
    //產生隨機的出生日期
    private static String getRandomDate(Random ran, boolean idNewID) {
        // TODO Auto-generated method stub
        if(ran==null){
            return "";
        }
        int year = 0;
        if(idNewID){
            year = 1900 + ran.nextInt(2007-1900);
        }else{
            year = 1 + ran.nextInt(99);
        }
        int month = 1+ran.nextInt(12);
        int day = 0;
        if(month==2){
            day= 1+ran.nextInt(28);
        }else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
            day= 1+ran.nextInt(31);
        }else{
            day= 1+ran.nextInt(30);
        }
        NumberFormat nf = NumberFormat.getIntegerInstance();
        nf.setMaximumIntegerDigits(2);
        nf.setMinimumIntegerDigits(2);
        String dateStr = Integer.toString(year)+nf.format(month)+nf.format(day);
        return dateStr;
    }
    //產生隨機的序列號
    private static String getIDOrder(Random ran) {
        // TODO Auto-generated method stub
        NumberFormat nf = NumberFormat.getIntegerInstance();
        nf.setMaximumIntegerDigits(3);
        nf.setMinimumIntegerDigits(3);
        if(ran==null){
            return "";
        }else{
            int order = 1+ran.nextInt(999);
            return nf.format(order);
        }
    }
    public Identity(){
        setWiBuffer();
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String randomID=Identity.getRandomIDCard(true);
        System.out.println("隨機身份證:"+randomID);
        /*
        String oldID="";
        String newID=Identity.getNewIDCard(oldID);
        System.out.println("舊身份證:"+oldID);
        System.out.println("新身份證:"+newID);
        String oldCard = Identity.getOldIDCard(newID);
        System.out.println("舊身份證:"+oldCard);
        /*
        String dateSource="2000-9-30";
        if(id.checkDate(dateSource))
            System.out.println("正確時間串:"+dateSource);
        else
            System.out.println("錯誤時間串:"+dateSource);
        */
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章