TypechoJoeTheme

Zt's Blog

【Java】本地运算身份证相关信息

Zt
2020-01-21
/
2 评论
/
863 阅读
/
正在检测是否收录...
01/21

闲得无聊,写了一个简单的本地的对身份证号进行运算的java小程序。
功能:
*通过iso验证算法、出生日期等相关信息验证身份证号真伪
*读取身份证号所属人出生地
*获取身份证号顺序码
*获取身份证号所属人出生日期

代码如下:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Idcard {
    public static int iso(String id) {
        int[] muiltiply = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

        int product = 0;

        for (int a = 0; a < 17; a++) {
            product += Integer.valueOf(id.substring(a, a + 1)) * muiltiply[a];
        }

        int T = 12 - (product % 11);

        int ending = T % 11;

        String confirm = id.substring(17,18);
        if (confirm.equals("X")) confirm = "10";

        if (Integer.valueOf(confirm) == ending) {
            return 1;
        } else {
            return -1;
        }
    }

    public static String post(String url, HashMap<String, String> paramsMap) {
        OkHttpClient mOkHttpClient = new OkHttpClient();
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        Set<String> keySet = paramsMap.keySet();
        String result = "";
        for (String key : keySet) {
            String value = paramsMap.get(key);
            formBodyBuilder.add(key, value);
        }
        FormBody formBody = formBodyBuilder.build();
        Request request = new Request
                .Builder()
                .post(formBody)
                .url(url)
                .build();
        try (Response response = mOkHttpClient.newCall(request).execute()) {
            result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String searchaddress(String province, String district, String city) {

        HashMap<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("keywords", province + "0000");
        paramsMap.put("subdistrict", "1");
        paramsMap.put("extensions", "base");
        paramsMap.put("key", "f944a58171423a3f97b18f2d11e2e48c");
        String provincename = JSONObject.parseObject(post("http://restapi.amap.com/v3/config/district", paramsMap)).getJSONArray("districts").getJSONObject(0).getString("name");

        paramsMap.put("keywords", province + district + "00");
        paramsMap.put("subdistrict", "1");
        paramsMap.put("extensions", "base");
        paramsMap.put("key", "f944a58171423a3f97b18f2d11e2e48c");
        String districtname = JSONObject.parseObject(post("http://restapi.amap.com/v3/config/district", paramsMap)).getJSONArray("districts").getJSONObject(0).getString("name");

        paramsMap.put("keywords", province + district + city);
        paramsMap.put("subdistrict", "1");
        paramsMap.put("extensions", "base");
        paramsMap.put("key", "f944a58171423a3f97b18f2d11e2e48c");
        String cityname = JSONObject.parseObject(post("http://restapi.amap.com/v3/config/district", paramsMap)).getJSONArray("districts").getJSONObject(0).getString("name");

        return provincename + districtname + cityname;

    }

    public static String birthday(String idcard) {
        String year = idcard.substring(6, 10);
        String month = idcard.substring(10, 12);
        String day = idcard.substring(12, 14);

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String nowyear = sdf.format(d);

        if ((Integer.valueOf(year) > Integer.valueOf(nowyear)) | (Integer.valueOf(month) > 12) | (Integer.valueOf(day) > 31)) return "-1";
        else return year + "年" + month + "月" + day + "日";
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String idnumber = sc.nextLine();
        if (idnumber.length() < 18){
            System.out.println("这是一张假的身份证");
            return;
        }
        int S = iso(idnumber);
        String birthday = "";
        if (birthday(idnumber).equals("-1")) {
            S = -1;
        } else {
            birthday = birthday(idnumber);
        }
        String provincecode = idnumber.substring(0, 2);
        String districtcode = idnumber.substring(2, 4);
        String citycode = idnumber.substring(4, 6);
        String address = searchaddress(provincecode, districtcode, citycode);
        String number = idnumber.substring(14, 17);
        if (S == -1) {
            System.out.println("这是一张假的身份证");
        } else {
            System.out.println("身份证号:" + idnumber + "\n出生地:" + address + "\n出生日期:" + birthday + "\n顺序码:" + number + "\n所有数据均通过本地运算得出,并非链接警方数据库!");
        }
    }
}

转载本博客所有内容均需遵守《知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议》,详细信息访问本博客的关于页面查看

测试图片:

真实身份证号成功读取相关信息


修改一位数字的假身份证号被识别

赞 · 0
版权属于:

Zt's Blog

本文链接:

https://www.zhen-t.com/blog/archives/7/(转载时请注明本文出处及文章链接)

评论 (2)
  1. Jane_Eyre 作者
    Windows 7 · Google Chrome

    震天,yyds

    2020-08-01 回复
  2. 李宇航 作者
    Android Pie · Google Chrome

    震天,加油啦
    你最棒的啦
    粉粉粉!

    2020-02-23 回复