java生成机器码

java根据系统参数生成每个计算机的唯一标识。

1. 获取CPU序列号

 /**
     * 获取CPU序列号
     * @return
     * @throws IOException
     */
    public static String getCPUSerialNumber() {
        String next;
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"wmic", "cpu", "get", "ProcessorId"});
            process.getOutputStream().close();
            Scanner sc = new Scanner(process.getInputStream());
            String serial = sc.next();
            next = sc.next();
        } catch (IOException e) {
            throw new RuntimeException("获取CPU序列号失败");
        }
        return next;
    }

2.获取 硬盘序列号

 /**
     * 获取 硬盘序列号(Windows)
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static String getHardDiskSerialNumber() {
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"wmic", "path", "win32_physicalmedia", "get", "serialnumber"});
            process.getOutputStream().close();
            Scanner sc = new Scanner(process.getInputStream());
            String serial = sc.next();
            return sc.next();
        } catch (IOException e) {
            throw new RuntimeException("获取硬盘序列号失败");
        }
    }

3. bois版本号(linux)

/**
     * bois版本号(linux)
     *
     * @return
     */
    public static String getBoisVersion() {
        String result = "";
        Process p;
        try {
            // 管道
            p = Runtime.getRuntime().exec("sudo dmidecode -s bios-version");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                result += line;
                break;
            }
            br.close();
        } catch (IOException e) {
            System.out.println("获取主板信息错误");
        }
        return result;
    }
View Code

4. 获取系统序列号(linux)

/**
     * 获取系统序列号(linux)
     *
     * @return
     */
    public static String getUUID() {
        String result = "";
        try {
            Process process = Runtime.getRuntime().exec("sudo dmidecode -s system-uuid");
            InputStream in;
            BufferedReader br;
            in = process.getInputStream();
            br = new BufferedReader(new InputStreamReader(in));
            while (in.read() != -1) {
                result = br.readLine();
            }
            br.close();
            in.close();
            process.destroy();
            System.out.println("获取序列号:"+result);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }
View Code

5.字符串分割

    private static String getSplitString(String str, String split, int length) {
        int len = str.length();
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < len; i++) {
            if (i % length == 0 && i > 0) {
                temp.append(split);
            }
            temp.append(str.charAt(i));
        }
        return temp.toString();
    }
View Code

6. 获取window or linux机器码

 /**
     * 获取window or linux机器码
     * @return
     */
    public static String getWindowNumber(String type){
        if (Objects.isNull(type)){
            return "";
        }
        Map<String,Object> codeMap=new HashMap<>(2);
        String result = "";
        if ("linux".equals(type)){
            String boisVersion = getBoisVersion();
            codeMap.put("boisVersion",boisVersion);
            System.out.println("boisVersion:" + boisVersion);
            String uuid = getUUID();
            codeMap.put("uuid",uuid);
            System.out.println("uuid:" + uuid);
            String codeMapStr = JSON.toJSONString(codeMap);
            String serials = MD5Util.saltingMD5(codeMapStr, SALT);
            result= getSplitString(serials, "-", 4);
        }else if ("window".equals(type)){
            String processorId = getCPUSerialNumber();
            codeMap.put("ProcessorId",processorId);
            System.out.println("ProcessorId:" + processorId);
            String serialNumber = getHardDiskSerialNumber();
            codeMap.put("SerialNumber",serialNumber);
            System.out.println("SerialNumber:" + serialNumber);
            String codeMapStr = JSON.toJSONString(codeMap);
            String serials = MD5Util.saltingMD5(codeMapStr, SALT);
            result= getSplitString(serials, "-", 4);
        }else {

        }
        return result;
    }

7.生成机器码

/**
     * 机器码:A45DF6C62F6568B6E0039653EF7FF55F
     * @param args
     */
    public static void main(String[] args) throws IOException {
        String windowNumber = getWindowNumber("window");
        System.out.println("机器码:" + windowNumber);
    }

 

posted @ 2022-09-05 15:12  sowler  阅读(2478)  评论(0)    收藏  举报