zoukankan      html  css  js  c++  java
  • Java获取机器码

    1.获取CPU序列号

    (1)windows系统第一种获取方式

    public class Test{
        public static void main(String[] args) throws IOException {
            System.out.println(getCPUSerial());
            System.exit(0);
        }
        
        /**获取本机CPU信息
         */
        private static String getCPUSerial() {
            String result = "";
            try {
                File file = File.createTempFile("tmp", ".vbs");//创建临时文件,路径为C:Documents and SettingsAdministratorLocal SettingsTemp
                file.deleteOnExit();
                FileWriter fw = new java.io.FileWriter(file);
                //是有vbs脚本语言,获取CPU唯一ID
                //表示程序出现运行时错误时,会继续运行,不中断
                StringBuilder sb = new StringBuilder("On Error Resume Next 
    
    ");
                //表示本机
                sb.append("strComputer = "."  
    ");
                //使用GetObject函数获取本机信息赋值给objWMIService
                sb.append("Set objWMIService = GetObject("winmgmts:" _ 
    ");
                sb.append("    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    ");
                sb.append("Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor")  
     ");
                //使用for循环取出CPU信息
                sb.append("For Each objItem in colItems
     " + "    Wscript.Echo objItem.ProcessorId  
     ");
                sb.append("    exit for  ' do the first cpu only! 
    ");
                sb.append("Next");
    
                fw.write(sb.toString());
                fw.close();
                Process p = Runtime.getRuntime().exec("cscript //NoLogo //T:10 " + file.getPath());
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = input.readLine()) != null) {
                    result += line;
                }
                input.close();
                file.delete();
            } catch (Exception e) {
                e.fillInStackTrace();
            }
            return result;
        }
    }
    View Code

      注:这种方式,即便是遇到错误也会执行下去。

    (2)windows系统第二种获取方式

    public class Test{
        public static void main(String[] args) throws IOException {
            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();
                System.out.println(serial);
            } catch (IOException e) {  
                e.printStackTrace();  
            }
        }
    }
    View Code

      注:这种方式获取比第一种要快。

    (3)linux系统获取方式

    public class Test{
        public static void main(String[] args) throws IOException {
            System.out.println(getCPUSerial());
        }
        /**获取本机CPU信息
         */
        private static String getCPUSerial() {
            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();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return result;
        }
    }
    View Code

      注:需要root用户来执行,如果使用普通用户执行的话需要输入当前用户的密码(普通用户不支持dmidecode命令 因为没权限)

    2.根据CPU序列号获取机器码

      :因为是在机器注册时使用,需要输入,所以就没有用MD5、RSA加密。

    public class Test{
        public static void main(String[] args) throws IOException {
            System.out.println(validate("BFCBFBFF000306C9"));
        }
        
        public static String validate(String CPUSerial) {
            if (CPUSerial == null || "".equals(CPUSerial.trim())) {
                return "";
            }
            byte[] snBytes = CPUSerial.getBytes();
            int sum = 0;
            for (byte snByte : snBytes) {
                sum += snByte;
            }
            int B = sum % 8;
            String text = Integer.toHexString(sum * sum);
            int length = text.length();
            String resultTemp = text.substring(0, 2) + text.substring(length - 2, length) + B;
            String result = "";
            char[] charArray = resultTemp.toCharArray();
            for (char c : charArray) {
                if (c == 0x31 || c == 0x3F || c == 0x4F) {
                    c++;
                }
                result += c;
            }
            return result.toUpperCase();
        }
    }
    View Code

      自定义简易的机器码,献丑了。

  • 相关阅读:
    C#单纯的字母数字ASCII码转换
    解析类型后加问号和双问号
    【转】composer autoload 自动加载性能优化指南
    【转】Laravel 三种中间件作用讲解
    【转】Laravel belongsTo 详解
    【转】docker-compose详解
    【转】laravel之Artisan命令操作Artisan Console
    【转】Shell中>/dev/null 2>&1 详解
    【转】docker-entrypoint.sh 文件的用处
    【转】解决Debian下sudo命令unable to initialize PAM问题
  • 原文地址:https://www.cnblogs.com/bl123/p/13739618.html
Copyright © 2011-2022 走看看