为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

[知识]身份证验证

2017-10-25 25页 doc 62KB 9阅读

用户头像

is_589748

暂无简介

举报
[知识]身份证验证[知识]身份证验证 java验证身份证号码及编码规则和提取相应信息 收藏 身份证前6位【ABCDEF】为行政区划数字代码(简称数字码)说明(参考《GB/T 2260-2007 中华人民共和国行政区划代码》):该数字码的编制原则和结构分析,它采用三层六位层次码结构,按层次分别表示我国各省(自治区,直辖市,特别行政区)、市(地区,自治州,盟)、县(自治县、县级市、旗、自治旗、市辖区、林区、特区)。 数字码码位结构从左至右的含义是: 第一层为AB两位代码表示省、自治区、直辖市、特别行政区; 第二层为CD两位代码表示市、地...
[知识]身份证验证
[知识]身份证验证 java验证身份证号码及编码规则和提取相应信息 收藏 身份证前6位【ABCDEF】为行政区划数字代码(简称数字码)说明(参考《GB/T 2260-2007 中华人民共和国行政区划代码》):该数字码的编制原则和结构分析,它采用三层六位层次码结构,按层次分别表示我国各省(自治区,直辖市,特别行政区)、市(地区,自治州,盟)、县(自治县、县级市、旗、自治旗、市辖区、林区、特区)。 数字码码位结构从左至右的含义是: 第一层为AB两位代码表示省、自治区、直辖市、特别行政区; 第二层为CD两位代码表示市、地区、自治州、盟、直辖市所辖市辖区、县汇总码、省(自治区)直辖县级行政区划汇总码,其中: ——01~20、51~70表示市,01、02还用于表示直辖市所辖市辖区、县汇总码; ——21~50表示地区、自治州、盟; ——90表示省(自治区)直辖县级行政区划汇总码。 第三层为EF两位表示县、自治县、县级市、旗、自治旗、市辖区、林区、特区,其中: ——01~20表示市辖区、地区(自治州、盟)辖县级市、市辖特区以及省(自治区)直辖县级行政区划中的县级市,01通常表示辖区汇总码; ——21~80表示县、自治县、旗、自治旗、林区、地区辖特区; ——81~99表示省(自治区)辖县级市。 身份证合法性校验:IdcardValidator.java Java代码 1. import java.text.ParseException; 2. import java.text.SimpleDateFormat; 3. import java.util.Calendar; 4. import java.util.Date; 5. import java.util.HashMap; 6. import java.util.Map; 7. import java.util.regex.Pattern; 8. 9. /** 10. *

11. * 类说明:身份证合法性校验 12. *

13. *

14. * --15位身份证号码:第7、8位为出生年份(两位数),第9、10位为出 生月份,第11、12位代表出生日期,第15位代表性别,奇数为男,偶数为女。 15. * --18位身份证号码:第7、8、9、10位为出生年份(四位数),第11、 第12位为出生月份,第13、14位代表出生日期,第17位代表性别,奇数为男, 偶数为女。 16. *

17. */ 18. @SuppressWarnings({"unchecked","unused","all"}) 19. public class IdcardValidator { 20. 21. /** 22. * 省,直辖市代码表: { 11:"北京",12:"天津",13:"河北",14:"山西",15:"内 蒙古", 23. * 21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏", 24. * 33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南", 25. * 42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆", 26. * 51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃", 27. * 63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国 外"} 28. */ 29. protected String codeAndCity[][] = { { "11", "北京" }, { "12", "天津 " }, 30. { "13", "河北" }, { "14", "山西" }, { "15", "内蒙古" }, { "21", "辽 宁" }, 31. { "22", "吉林" }, { "23", "黑龙江" }, { "31", "上海" }, { "32", "江 苏" }, 32. { "33", "浙江" }, { "34", "安徽" }, { "35", "福建" }, { "36", "江西 " }, 33. { "37", "山东" }, { "41", "河南" }, { "42", "湖北" }, { "43", "湖南 " }, 34. { "44", "广东" }, { "45", "广西" }, { "46", "海南" }, { "50", "重庆 " }, 35. { "51", "四川" }, { "52", "贵州" }, { "53", "云南" }, { "54", "西藏 " }, 36. { "61", "陕西" }, { "62", "甘肃" }, { "63", "青海" }, { "64", "宁夏 " }, 37. { "65", "新疆" }, { "71", "台湾" }, { "81", "香港" }, { "82", "澳门 " }, 38. { "91", "国外" } }; 39. 40. private String cityCode[] = { "11", "12", "13", "14", "15", "21 ", "22", 41. "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", 42. "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", 43. "64", "65", "71", "81", "82", "91" }; 44. 45. // 每位加权因子 46. private int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 1 0, 5, 8, 4, 2 }; 47. 48. // 第18位校检码 49. private String verifyCode[] = { "1", "0", "X", "9", "8", "7", "6", "5", 50. "4", "3", "2" }; 51. 52. 53. /** 54. * 验证所有的身份证的合法性 55. * @param idcard 56. * @return 57. */ 58. public boolean isValidatedAllIdcard(String idcard) { 59. if (idcard.length() == 15) { 60. idcard = this.convertIdcarBy15bit(idcard); 61. } 62. return this.isValidate18Idcard(idcard); 63. } 64. 65. /** 66. *

67. * 判断18位身份证的合法性 68. *

69. * 根据〖中华人民共和国国家标准GB11643-1999〗中有关公民身份号码的 规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。 70. * 排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数 字顺序码和一位数字校验码。 71. *

72. * 顺序码: 表示在同一地址码所标识的区域范围内,对同年、同月、同 日出生 的人编定的顺序号,顺序码的奇数分配给男性,偶数分配 给女性。 73. *

74. *

75. * 1.前1、2位数字表示:所在省份的代码; 2.第3、4位数字表示:所在城 市的代码; 3.第5、6位数字表示:所在区县的代码; 76. * 4.第7~14位数字表示:出生年、月、日; 5.第15、16位数字表示:所在 地的派出所的代码; 77. * 6.第17位数字表示性别:奇数表示男性,偶数表示女性; 78. * 7.第18位数字是校检码:也有的说是个人信息码,一般是随计算机的随机产 生,用来检验身份证的正确性。校检码可以是0~9的数字,有时也用x表示。 79. *

80. *

81. * 第十八位数字(校验码)的计算方法为: 1.将前面的身份证号码17位数分别 乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 82. * 2 1 6 3 7 9 10 5 8 4 2 83. *

84. *

85. * 2.将这17位数字和系数相乘的结果相加。 86. *

87. *

88. * 3.用加出来和除以11,看余数是多少, 89. *

90. * 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后 一位身份证的号码为1 0 X 9 8 7 6 5 4 3 91. * 2。 92. *

93. * 5.通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字 的?。如果余数是10,身份证的最后一位号码就是2。 94. *

95. * 96. * @param idcard 97. * @return 98. */ 99. public boolean isValidate18Idcard(String idcard) { 100. // 非18位为假 101. if (idcard.length() != 18) { 102. return false; 103. } 104. // 获取前17位 105. String idcard17 = idcard.substring(0, 17); 106. // 获取第18位 107. String idcard18Code = idcard.substring(17, 18); 108. char c[] = null; 109. String checkCode = ""; 110. // 是否都为数字 111. if (isDigital(idcard17)) { 112. c = idcard17.toCharArray(); 113. } else { 114. return false; 115. } 116. 117. if (null != c) { 118. int bit[] = new int[idcard17.length()]; 119. 120. bit = converCharToInt(c); 121. 122. int sum17 = 0; 123. 124. sum17 = getPowerSum(bit); 125. 126. // 将和值与11取模得到余数进行校验码判断 127. checkCode = getCheckCodeBySum(sum17); 128. if (null == checkCode) { 129. return false; 130. } 131. // 将身份证的第18位与算出来的校码进行匹配,不相等就为假 132. if (!idcard18Code.equalsIgnoreCase(checkCode)) { 133. return false; 134. } 135. } 136. return true; 137. } 138. 139. /** 140. * 验证15位身份证的合法性,该方法验证不准确,最好是将15转为18位后 再判断,该类中已提供。 141. * 142. * @param idcard 143. * @return 144. */ 145. public boolean isValidate15Idcard(String idcard) { 146. // 非15位为假 147. if (idcard.length() != 15) { 148. return false; 149. } 150. 151. // 是否全都为数字 152. if (isDigital(idcard)) { 153. String provinceid = idcard.substring(0, 2); 154. String birthday = idcard.substring(6, 12); 155. int year = Integer.parseInt(idcard.substring(6, 8)); 156. int month = Integer.parseInt(idcard.substring(8, 10)); 157. int day = Integer.parseInt(idcard.substring(10, 12)); 158. 159. // 判断是否为合法的省份 160. boolean flag = false; 161. for (String id : cityCode) { 162. if (id.equals(provinceid)) { 163. flag = true; 164. break; 165. } 166. } 167. if (!flag) { 168. return false; 169. } 170. // 该身份证生出日期在当前日期之后时为假 171. Date birthdate = null; 172. try { 173. birthdate = new SimpleDateFormat("yyMMdd").parse(birt hday); 174. } catch (ParseException e) { 175. e.printStackTrace(); 176. } 177. if (birthdate == null || new Date().before(birthdate)) { 178. return false; 179. } 180. 181. // 判断是否为合法的年份 182. GregorianCalendar curDay = new GregorianCalendar(); 183. int curYear = curDay.get(Calendar.YEAR); 184. int year2bit = Integer.parseInt(String.valueOf(curYear) 185. .substring(2)); 186. 187. // 判断该年份的两位表示法,小于50的和大于当前年份的,为假 188. if ((year < 50 && year > year2bit)) { 189. return false; 190. } 191. 192. // 判断是否为合法的月份 193. if (month < 1 || month > 12) { 194. return false; 195. } 196. 197. // 判断是否为合法的日期 198. boolean mflag = false; 199. curDay.setTime(birthdate); //将该身份证的出生日期赋于对象cur Day 200. switch (month) { 201. case 1: 202. case 3: 203. case 5: 204. case 7: 205. case 8: 206. case 10: 207. case 12: 208. mflag = (day >= 1 && day <= 31); 209. break; 210. case 2: //公历的2月非闰年有28天,闰年的2月是29天。 211. if (curDay.isLeapYear(curDay.get(Calendar.YEAR))) { 212. mflag = (day >= 1 && day <= 29); 213. } else { 214. mflag = (day >= 1 && day <= 28); 215. } 216. break; 217. case 4: 218. case 6: 219. case 9: 220. case 11: 221. mflag = (day >= 1 && day <= 30); 222. break; 223. } 224. if (!mflag) { 225. return false; 226. } 227. } else { 228. return false; 229. } 230. return true; 231. } 232. 233. /** 234. * 将15位的身份证转成18位身份证 235. * 236. * @param idcard 237. * @return 238. */ 239. public String convertIdcarBy15bit(String idcard) { 240. String idcard17 = null; 241. // 非15位身份证 242. if (idcard.length() != 15) { 243. return null; 244. } 245. 246. if (isDigital(idcard)) { 247. // 获取出生年月日 248. String birthday = idcard.substring(6, 12); 249. Date birthdate = null; 250. try { 251. birthdate = new SimpleDateFormat("yyMMdd").parse(birt hday); 252. } catch (ParseException e) { 253. e.printStackTrace(); 254. } 255. Calendar cday = Calendar.getInstance(); 256. cday.setTime(birthdate); 257. String year = String.valueOf(cday.get(Calendar.YEAR)); 258. 259. idcard17 = idcard.substring(0, 6) + year + idcard.substring (8); 260. 261. char c[] = idcard17.toCharArray(); 262. String checkCode = ""; 263. 264. if (null != c) { 265. int bit[] = new int[idcard17.length()]; 266. 267. // 将字符数组转为整型数组 268. bit = converCharToInt(c); 269. int sum17 = 0; 270. sum17 = getPowerSum(bit); 271. 272. // 获取和值与11取模得到余数进行校验码 273. checkCode = getCheckCodeBySum(sum17); 274. // 获取不到校验位 275. if (null == checkCode) { 276. return null; 277. } 278. 279. // 将前17位与第18位校验码拼接 280. idcard17 += checkCode; 281. } 282. } else { // 身份证包含数字 283. return null; 284. } 285. return idcard17; 286. } 287. 288. /** 289. * 15位和18位身份证号码的基本数字和位数验校 290. * 291. * @param idcard 292. * @return 293. */ 294. public boolean isIdcard(String idcard) { 295. return idcard == null || "".equals(idcard) ? false : Pattern.mat ches( 296. "(^\\d{15}$)|(\\d{17}(?:\\d|x|X)$)", idcard); 297. } 298. 299. /** 300. * 15位身份证号码的基本数字和位数验校 301. * 302. * @param idcard 303. * @return 304. */ 305. public boolean is15Idcard(String idcard) { 306. return idcard == null || "".equals(idcard) ? false : Pattern.mat ches( 307. "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3} $", 308. idcard); 309. } 310. 311. /** 312. * 18位身份证号码的基本数字和位数验校 313. * 314. * @param idcard 315. * @return 316. */ 317. public boolean is18Idcard(String idcard) { 318. return Pattern 319. .matches( 320. "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2] \\d)|3[0-1])\\d{3}([\\d|x|X]{1})$", 321. idcard); 322. } 323. 324. /** 325. * 数字验证 326. * 327. * @param str 328. * @return 329. */ 330. public boolean isDigital(String str) { 331. return str == null || "".equals(str) ? false : str.matches("^[0- 9]*$"); 332. } 333. 334. /** 335. * 将身份证的每位和对应位的加权因子相乘之后,再得到和值 336. * 337. * @param bit 338. * @return 339. */ 340. public int getPowerSum(int[] bit) { 341. 342. int sum = 0; 343. 344. if (power.length != bit.length) { 345. return sum; 346. } 347. 348. for (int i = 0; i < bit.length; i++) { 349. for (int j = 0; j < power.length; j++) { 350. if (i == j) { 351. sum = sum + bit[i] * power[j]; 352. } 353. } 354. } 355. return sum; 356. } 357. 358. /** 359. * 将和值与11取模得到余数进行校验码判断 360. * 361. * @param checkCode 362. * @param sum17 363. * @return 校验位 364. */ 365. public String getCheckCodeBySum(int sum17) { 366. String checkCode = null; 367. switch (sum17 % 11) { 368. case 10: 369. checkCode = "2"; 370. break; 371. case 9: 372. checkCode = "3"; 373. break; 374. case 8: 375. checkCode = "4"; 376. break; 377. case 7: 378. checkCode = "5"; 379. break; 380. case 6: 381. checkCode = "6"; 382. break; 383. case 5: 384. checkCode = "7"; 385. break; 386. case 4: 387. checkCode = "8"; 388. break; 389. case 3: 390. checkCode = "9"; 391. break; 392. case 2: 393. checkCode = "x"; 394. break; 395. case 1: 396. checkCode = "0"; 397. break; 398. case 0: 399. checkCode = "1"; 400. break; 401. } 402. return checkCode; 403. } 404. 405. /** 406. * 将字符数组转为整型数组 407. * 408. * @param c 409. * @return 410. * @throws NumberFormatException 411. */ 412. public int[] converCharToInt(char[] c) throws NumberFormatExc eption { 413. int[] a = new int[c.length]; 414. int k = 0; 415. for (char temp : c) { 416. a[k++] = Integer.parseInt(String.valueOf(temp)); 417. } 418. return a; 419. } 420. 421. public static void main(String[] args) throws Exception { 422. String idcard15 = ""; 423. String idcard18 = ""; 424. IdcardValidator iv = new IdcardValidator(); 425. boolean flag = false; 426. flag = iv.isValidate18Idcard(idcard18); 427. System.out.println(flag); 428. 429. flag = iv.isValidate15Idcard(idcard15); 430. System.out.println(flag); 431. 432. System.out.println(iv.convertIdcarBy15bit(idcard15)); 433. flag = iv.isValidate18Idcard(iv.convertIdcarBy15bit(idcard15)); 434. System.out.println(flag); 435. 436. System.out.println(iv.isValidatedAllIdcard(idcard18)); 437. 438. } 439. } 提取身份证相关信息:IdcardInfoExtractor.java Java代码 1. import java.text.SimpleDateFormat; 2. import java.util.Calendar; 3. import java.util.Date; 4. import java.util.GregorianCalendar; 5. import java.util.HashMap; 6. import java.util.Map; 7. import java.util.Set; 8. 9. /** 10. *

11. * 类说明:提取身份证相关信息 12. *

13. */ 14. public class IdcardInfoExtractor { 15. // 省份 16. private String province; 17. // 城市 18. private String city; 19. // 区县 20. private String region; 21. // 年份 22. private int year; 23. // 月份 24. private int month; 25. // 日期 26. private int day; 27. // 性别 28. private String gender; 29. // 出生日期 30. private Date birthday; 31. 32. private Map cityCodeMap = new HashMap() { 33. { 34. this.put("11", "北京"); 35. this.put("12", "天津"); 36. this.put("13", "河北"); 37. this.put("14", "山西"); 38. this.put("15", "内蒙古"); 39. this.put("21", "辽宁"); 40. this.put("22", "吉林"); 41. this.put("23", "黑龙江"); 42. this.put("31", "上海"); 43. this.put("32", "江苏"); 44. this.put("33", "浙江"); 45. this.put("34", "安徽"); 46. this.put("35", "福建"); 47. this.put("36", "江西"); 48. this.put("37", "山东"); 49. this.put("41", "河南"); 50. this.put("42", "湖北"); 51. this.put("43", "湖南"); 52. this.put("44", "广东"); 53. this.put("45", "广西"); 54. this.put("46", "海南"); 55. this.put("50", "重庆"); 56. this.put("51", "四川"); 57. this.put("52", "贵州"); 58. this.put("53", "云南"); 59. this.put("54", "西藏"); 60. this.put("61", "陕西"); 61. this.put("62", "甘肃"); 62. this.put("63", "青海"); 63. this.put("64", "宁夏"); 64. this.put("65", "新疆"); 65. this.put("71", "台湾"); 66. this.put("81", "香港"); 67. this.put("82", "澳门"); 68. this.put("91", "国外"); 69. } 70. }; 71. 72. private IdcardValidator validator = null; 73. 74. /** 75. * 通过构造方法初始化各个成员属性 76. */ 77. public IdcardInfoExtractor(String idcard) { 78. try { 79. validator = new IdcardValidator(); 80. if (validator.isValidatedAllIdcard(idcard)) { 81. if (idcard.length() == 15) { 82. idcard = validator.convertIdcarBy15bit(idcard); 83. } 84. // 获取省份 85. String provinceId = idcard.substring(0, 2); 86. Set key = this.cityCodeMap.keySet(); 87. for (String id : key) { 88. if (id.equals(provinceId)) { 89. this.province = this.cityCodeMap.get(id); 90. break; 91. } 92. } 93. 94. // 获取性别 95. String id17 = idcard.substring(16, 17); 96. if (Integer.parseInt(id17) % 2 != 0) { 97. this.gender = "男"; 98. } else { 99. this.gender = "女"; 100. } 101. 102. // 获取出生日期 103. String birthday = idcard.substring(6, 14); 104. Date birthdate = new SimpleDateFormat("yyyyMMdd") 105. .parse(birthday); 106. this.birthday = birthdate; 107. GregorianCalendar currentDay = new GregorianCalendar(); 108. currentDay.setTime(birthdate); 109. this.year = currentDay.get(Calendar.YEAR); 110. this.month = currentDay.get(Calendar.MONTH) + 1; 111. this.day = currentDay.get(Calendar.DAY_OF_MONTH); 112. } 113. } catch (Exception e) { 114. e.printStackTrace(); 115. } 116. } 117. 118. /** 119. * @return the province 120. */ 121. public String getProvince() { 122. return province; 123. } 124. 125. /** 126. * @return the city 127. */ 128. public String getCity() { 129. return city; 130. } 131. 132. /** 133. * @return the region 134. */ 135. public String getRegion() { 136. return region; 137. } 138. 139. /** 140. * @return the year 141. */ 142. public int getYear() { 143. return year; 144. } 145. 146. /** 147. * @return the month 148. */ 149. public int getMonth() { 150. return month; 151. } 152. 153. /** 154. * @return the day 155. */ 156. public int getDay() { 157. return day; 158. } 159. 160. /** 161. * @return the gender 162. */ 163. public String getGender() { 164. return gender; 165. } 166. 167. /** 168. * @return the birthday 169. */ 170. public Date getBirthday() { 171. return birthday; 172. } 173. 174. @Override 175. public String toString() { 176. return "省份:" + this.province + ",性别:" + this.gender + ",出 生日期:" 177. + this.birthday; 178. } 179. 180. public static void main(String[] args) { 181. String idcard = ""; 182. IdcardInfoExtractor ie = new IdcardInfoExtractor(idcard); 183. System.out.println(ie.toString()); 184. } 185. }
/
本文档为【[知识]身份证验证】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索