1. 程式人生 > >Java實現身份證號碼驗證源碼分享

Java實現身份證號碼驗證源碼分享

分隔 toc bst nbsp port birt random exti 一位數

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 身份證驗證的工具(支持15位或18位省份證)
 * 身份證號碼結構:
 * <p>
 * 根據〖中華人民共和國國家標準GB11643-1999〗中有關公民身份號碼的規定,公民身份號碼是特征組合碼,由十七位數字本體碼和一位數字校驗碼組成。
 * 排列順序從左至右依次為:6位數字地址碼,8位數字出生日期碼,3位數字順序碼和1位數字校驗碼。
 * <p>
 * 地址碼(前6位):表示對象常住戶口所在縣(市、鎮、區)的行政區劃代碼,按GB/T2260的規定執行。
 * <li>前1、2位數字表示:所在省份的代碼;</li>
 * <li>第3、4位數字表示:所在城市的代碼;</li>
 * <li>第5、6位數字表示:所在區縣的代碼;</li>
 * <p>
 * 出生日期碼,(第7位 - 14位):表示編碼對象出生年、月、日,按GB按GB/T7408的規定執行,年、月、日代碼之間不用分隔符。
 * <p>
 * 順序碼(第15位至17位):表示在同一地址碼所標示的區域範圍內,對同年、同月、同日出生的人編訂的順序號,順序碼的奇數分配給男性,偶數分配給女性。
 * <li>第15、16位數字表示:所在地的派出所的代碼;</li>
 * <li>第17位數字表示性別:奇數表示男性,偶數表示女性;</li>
 * <li>第18位數字是校檢碼:也有的說是個人信息碼,一般是隨計算機的隨機產生,用來檢驗身份證的正確性。校檢碼可以是0~9的數字,有時也用x表示。</li>
 * <p>
 * 校驗碼(第18位數):
 * <p>
 * 十七位數字本體碼加權求和公式 s = sum(Ai*Wi), i = 0..16,先對前17位數字的權求和;
 * Ai:表示第i位置上的身份證號碼數字值.Wi:表示第i位置上的加權因子.Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2;
 * 計算模 Y = mod(S, 11)
 * 通過模得到對應的模 Y: 0 1 2 3 4 5 6 7 8 9 10 校驗碼: 1 0 X 9 8 7 6 5 4 3 2
 * <p>
 * 計算步驟:
 * 1.將前17位數分別乘以不同的系數。從第1位到第17位的系數分別為:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
 * 2.將這17位數字和系數相乘的結果相加。
 * 3.用加出來和除以11,看余數是多少
 * 4.余數只可能有0 1 2 3 4 5 6 7 8 9 10這11個數字,分別對應的最後一位身份證的號碼為:1 0 X 9 8 7 6 5 4 3
 * <p>
 
*/ public class IDCardUtil { /** * <pre> * 省、直轄市代碼表: * 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 : 國外 * </pre>
*/ final static String CITY_CODE[] = {"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"}; /** * 效驗碼 */ final static char[] PARITYBIT = {‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘};
/** * 加權因子 * Math.pow(2, i - 1) % 11 */ final static int[] POWER = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; /** * 身份證驗證 * * @param id 號碼內容 * @return 是否有效 */ public final static boolean isValid(String id) { if (id == null) return false; int len = id.length(); if (len != 15 && len != 18) return false; //校驗區位碼 if (!validCityCode(id.substring(0, 2))) return false; //校驗生日 if (!validDate(id)) return false; if (len == 15) return true; //校驗位數 return validParityBit(id); } private static boolean validParityBit(String id) { char[] cs = id.toUpperCase().toCharArray(); int power = 0; for (int i = 0; i < cs.length; i++) { //最後一位可以是X if (i == cs.length - 1 && cs[i] == ‘X‘) break; // 非數字 if (cs[i] < ‘0‘ || cs[i] > ‘9‘) return false; // 加權求和 if (i < cs.length - 1) { power += (cs[i] - ‘0‘) * POWER[i]; } } return PARITYBIT[power % 11] == cs[cs.length - 1]; } private static boolean validDate(String id) { try { String birth = id.length() == 15 ? "19" + id.substring(6, 12) : id.substring(6, 14); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date birthDate = sdf.parse(birth); if (!birth.equals(sdf.format(birthDate))) return false; } catch (ParseException e) { return false; } return true; } private static boolean validCityCode(String cityCode) { for (String code : CITY_CODE) { if (code.equals(cityCode)) return true; } return false; } /** * 將15位的身份證轉成18位身份證 * * @param id * @return */ final public static String id15To18(String id) { if (id == null || id.length() != 15) return null; if (!isValid(id)) return null; String id17 = id.substring(0, 6) + "19" + id.substring(6); int power = 0; char[] cs = id17.toCharArray(); for (int i = 0; i < cs.length; i++) { power += (cs[i] - ‘0‘) * POWER[i]; } // 將前17位與第18位校驗碼拼接 return id17 + String.valueOf(PARITYBIT[power % 11]); } /** * 生成隨機整數 * <p> * * @param min * @param max * @return */ public static int rand(int min, int max) { Random random = new Random(); return random.nextInt(max + 1) % (max - min + 1) + min; } public final static String generateID() { // 地址碼 String body = CITY_CODE[rand(0, CITY_CODE.length - 1)] + "0101"; // 出生年 String y = String.valueOf(rand(1950, Calendar.getInstance().get(Calendar.YEAR))); String m = String.valueOf(rand(1, 12)); if (m.length() == 1) m = "0" + m; String d = String.valueOf(rand(1, 28)); if (d.length() == 1) d = "0" + d; String idx = String.valueOf(rand(1, 999)); if (idx.length() == 1) idx = "00" + idx; else if (idx.length() == 2) idx = "0" + idx; body += y + m + d + idx; // 累加body部分與位置加權的積 int power = 0; char[] cs = body.toCharArray(); for (int i = 0; i < cs.length; i++) { power += (cs[i] - ‘0‘) * POWER[i]; } // 得出校驗碼 return body + String.valueOf(PARITYBIT[power % 11]); } }

原文地址:http://www.jianshu.com/p/ff32a462947f

Java實現身份證號碼驗證源碼分享