• 18位身份证验证


    18位身份证验证

     

     

     

     

    package com.yhj.common.idValidator.idchecker.addrs;

     

     

     

    import java.io.BufferedReader;

    import java.io.FileNotFoundException;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

     

     

    /**

     * 地址码读取类

     *

     */

    public class DicReader {

       

        /**

         * 读取地址码

         * @param addrNum

         * @return 若存在,则返回该地址码对应的地址名称,若不存在,返回null

         */

        public static String readAddress(String addrNum) {

           char first = addrNum.charAt(0);

           if(first == '1' || first == '2' || first == '3' || first == '4' || first == '5' || first == '6'){

               String filePath = first + ".dic";

               String addr =  readAddress(filePath, "UTF-8", addrNum);

               return addr;

           }

           return "非中国居民";

        }

       

        /**

         * 读取地址码是否存在

         * @param filePath 文件路径

         * @param charset 文件编码

         * @param addrNum 地址码,6位数字

         * @return 存在返回地址名称,否则返回null

         */

        public static String readAddress(String filePath, String charset, String addrNum){

           String addr = null;

           try {

               InputStream is = DicReader.class.getResourceAsStream(filePath);

               BufferedReader buffReader = new BufferedReader(new InputStreamReader(is,charset));

     

               String line;

               while((line = buffReader.readLine()) != null){

                  if(addrNum.equals(line.substring(0,6))) {

                      addr = line.substring(7, line.length());

                      break;

                  }

               }

               buffReader.close();

              

           } catch (FileNotFoundException e) {

               System.err.println("找到不地址码文件");

               e.printStackTrace();

           } catch (IOException e) {

               System.err.println("读取地址码文件失败");

               e.printStackTrace();

           }

           return addr;

        }

     

    }

    package com.yhj.common.idValidator.idchecker.check;

     

     

     

    import java.util.HashSet;

    import java.util.Set;

     

    import com.yhj.common.idValidator.idchecker.addrs.DicReader;

    import com.yhj.common.idValidator.idchecker.entity.ID;

     

    /**

     * 身份证检验类

     *

     */

    public class Checker {

       

        private ID id;       //身份证号码

        private String errorMsg = "";   //非法反馈信息

        private Set<String> errorMsgs = new HashSet<String>();  //非法信息集合

        private String addr = "";       //地址信息

       

        /**

         * 构造方法

         * @param num 身份证字符串,18

         */

        public Checker(String num) {

           if(num.length()>=18) {

               this.id = new ID(num);

           } else {

               this.id = new ID("0000000000000000000");

           }

     

        }

       

        /**

         * 总的身份证验证

         * 验证顺序:长度 -> 生日 -> 最后一位校验码 -> 地址

         * @return 若遇到有一项目不合法即返回false,所有验证通过才返回true

         */

        public boolean check(){

           boolean right = false;

           right = checkLength();      //验证长度

           if(right){

               right = checkBirth();       //验证生日

               if(right) {

                  right = checkCheckCode();       //验证最后一校验码

                  if(right) {

                      right = checkAddr();     //验证地址

                  }

               }

           }

           return right;

        }

       

        /**

         * 返回该用户的Id是否合法对应的中文

         */

        public String checkIdCode() {

           return check()?"":"";

        }

       

        /**

         * 总的身份证验证

         * 验证项目包括长度、地址、生日、最后一位校验码

         * @return 身份证合法则返回true,否则false

         */

        public boolean checkAll(){

           if(!checkLength()) {     //验证长度

               errorMsgs.add(errorMsg);

               return false;        //长度都不对,其他的就不用验证了

           }

           if(!checkAddr()){

               errorMsgs.add(errorMsg); //验证地址

           }

           if(!checkBirth()){

               errorMsgs.add(errorMsg); //验证生日

           }

           if(!checkCheckCode()){          //验证最后一位校验码

               errorMsgs.add(errorMsg);

           }

           return errorMsgs.size()==0 ? true : false;

        }

       

        /**

         * 检查身份证长度是否正确

         * @return 长度为18则返回true,否则返回false

         */

        public boolean checkLength(){

           int length = id.getNum().length();

           if(length == 18) {

               return true;

           }

           errorMsg = "身份证长度不正确" ;

           return false;

        }

       

        /**

         * 验证身份证出生年月日是否合法

         * @return 合法返回true,否则返回false

         */

        public boolean checkBirth(){

           String birth = id.getBirth();

           //System.out.println(birth);

           int year, month, day;       //年月日

           try{

               year = Integer.valueOf(birth.substring(0,4));

               month = Integer.valueOf(birth.substring(4,6));

               day = Integer.valueOf(birth.substring(6, 8));

           } catch (NumberFormatException e) {

               errorMsg = "身份证生日码不正确!";

               return false;

           }

           if((year >= 1900 && year <= 2020) && (month >=1 && month <= 12) && (day >= 1 && day <= 31)) {

               return true;

           }

           errorMsg = "身份证生日码不正确!";

           return false;

        }

       

        /**

         * 验证地址码是否存在

         * @return 存在返回true,不存在返回false

         */

        public boolean checkAddr(){

           String addrCode = id.getAddr();

           //System.out.println("addrcode = " + addrCode);

           addr = DicReader.readAddress(addrCode);

           if(addr != null) {

               return true;

           }

           errorMsg = "身份证地址码不正确!";

           return false;

        }

     

        /**

         * 验证校验码是否正确

         * @return 正确返回true,否则返回false

         */

        public boolean checkCheckCode(){

           String chCode = id.caculateCheckCode();       //计算正确的末位校验码

           if(id.getCheck().equalsIgnoreCase(chCode)){

               return true;

           }

           errorMsg = "身份证校验码不正确, 正确的校验码是 " + chCode;

           return false;

        }

     

       

        /**

         * 获得出生年月日

         * @return

         */

        public String getBirth(){

           return id.getFormatBirth();

        }

       

        /**

         * 获得地址

         * @return

         */

        public String getAddr(){

           if("".equals(addr)) {

               checkAddr();

           }

           if(null==addr){

               addr="非法的中国公民!";

           }

           return addr;

        }

       

        /**

         * 获取性别

         * @return

         */

        public String getSex(){

           return id.getSex();

        }

       

        /**

         * 取得错误信息

         * @return

         */

        public String getErrorMsg(){

           return errorMsg;

        }

       

        /**

         * 取得错误信息集合

         * @return

         */

        public Set<String> getErrorMsgs() {

           return errorMsgs;

        }

       

        /**

         * 取得身份证对象

         * @return

         */

        public ID getId() {

           return id;

        }

     

     

     

       

    }

     

    package com.yhj.common.idValidator.idchecker.check;

     

    /**

     * com.yhj.common.idValidator.idchecker.check.checkID.java

     * be created by 2010-1-6 下午07:58:01+

     * 本类的主要功能:

     * <p>

     * @author 一线天色 天宇星辰

     * 备注信息:

     * </p>

     *

     */

    public class CheckID {

        /**

         * 检测身份证号码的正确性

         */

        public static void checkId(String id) {

           Checker checker = new Checker(id);

           if(!checker.checkAll()){

               System.out.println("校验过程中发现了 身份证号码 错误!以下的信息将不能保证其正确性! ==================================");

           }

           System.out.println("出生年月日  : " + checker.getBirth());

           System.out.println("性别  " + checker.getSex());

           System.out.println("居民地址  : " + checker.getAddr());

           System.out.println("身份证号码是否合法 : " +checker.checkIdCode()+ "!");

           if(!checker.getErrorMsgs().isEmpty()){

               System.out.print("错误信息:");

               for(String msg : checker.getErrorMsgs()) {

                  System.out.print(msg);

               }

           }

        }

    }

     

    package com.yhj.common.idValidator.idchecker.entity;

     

     

     

    /**

     * 身份证号码

     * 例如:320105198209275127 ->

     * addr   birth    seq  check

     * 320105 19820927 512  7

     *

     */

    public class ID {

       

        private String num;      //全长身份证号码

        private String addr; //地址

        private String birth;    //生日

        private String seq;      //序号

        private String check;    //校验码

       

       

     

        /**

         * 构造方法 1

         * @param num 格式如:"320105198209275127"

         */

        public ID(String num) {

           this.num = num;

           this.addr = num.substring(0, 6);

           this.birth = num.substring(6, 14);

           this.seq = num.substring(14,17);

           this.check = num.substring(17,18);

        }

       

        /**

         * 构造方法 2

         * @param addr 地址码, 格式如:"320105"

         * @param birth 生日码,格式如:"19820927"

         * @param seq 顺序码,格式如:"512"

         * @param check 校验码,格式如:"7"

         */

        public ID(String addr, String birth, String seq, String check) {

           this.addr = addr;

           this.birth = birth;

           this.seq = seq;

           this.check = check;

           this.num = addr + birth + seq + check;

        }

     

        /**

         * 分离身份证号码

         * @return 字符数组

         */

        public char[] separate(){

           return this.num.toCharArray();

        }

       

        /**

         * 分离身份证号码

         * @return 整型数组,最后一位若是'X',则返回10

         */

        public int[] separate2int(){

           int length = 18;

           int[] ins = new int[length];

           int i = 0;

           for( ; i < length - 1; i ++) {

               ins[i] = Integer.valueOf(num.substring(i, i+1));

           }

           String last = num.substring(i, i+1);

           ins[i] = "X".equals(last) ? 10 : Integer.valueOf(last);

           return ins;

        }

       

        /**

         * 计算校验位

         * @return

         */

        public String caculateCheckCode(){

           int total = 0;       //校验值和

           int length = 18;  //身份证长度

           int[] ins = new int[length];

           int[] checkCodes = Checkcode.checkCodes;

           int i = 0;

           try{

               for( ; i < length - 1; i ++) {

                  ins[i] = Integer.valueOf(num.substring(i, i+1));

                  total += (ins[i]*checkCodes[i]);

               }

           } catch(NumberFormatException e) {

               return null;

           }

          

           int modResult = total % 11;

           return Checkcode.checkResult(modResult);

          

        }

       

        /**

         * 获取中文格式的出生年月日

         * @return

         */

        public String getFormatBirth(){

           return birth.substring(0, 4) + "" + birth.substring(4,6) + "" + birth.substring(6,8) + "";

        }

       

        /**

         * 获取性别

         * @return

         */

        public String getSex() {

           return seq.charAt(2)%2 == 0 ? "" : "";

        }

       

        public String getAddr() {

           return addr;

        }

     

        public void setAddr(String addr) {

           this.addr = addr;

        }

     

        public String getBirth() {

           return birth;

        }

     

        public void setBirth(String birth) {

           this.birth = birth;

        }

     

        public String getSeq() {

           return seq;

        }

     

        public void setSeq(String seq) {

           this.seq = seq;

        }

     

        public String getCheck() {

           return check;

        }

     

        public void setCheck(String check) {

           this.check = check;

        }

       

     

        public String getNum() {

           return num;

        }

     

        public void setNum(String num) {

           this.num = num;

        }

       

     

    }

     

    package com.yhj.common.idValidator.idchecker.test;

     

    import com.yhj.common.idValidator.idchecker.check.CheckID;

     

    public class TestChecker {

     

        public static void main(String[] args) {

           CheckID.checkId("411324202012317890");

        }

    }

     

  • 相关阅读:
    Linux C语言编程基础
    《信息安全系统设计与实现》学习笔记二
    《信息安全系统设计与实现》选做一
    《信息安全系统设计与实现》学习笔记一
    商用密码调研
    20191212斯廷响 2020-2021-2 《Python程序设计》实验四报告
    20191212 2020-2021-2 《Python程序设计》实验三报告
    20191212 2020-2021-2 《Python程序设计》实验二报告
    20191212 2020-2021-2 《Python程序设计》实验一报告
    学习笔记2
  • 原文地址:https://www.cnblogs.com/alaricblog/p/3278339.html
Copyright © 2020-2023  润新知