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 Settings\Administrator\Local Settings\Temp
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            //是有vbs腳本語言,獲取CPU唯一ID
            //表示程序出現運行時錯誤時,會繼續運行,不中斷
            StringBuilder sb = new StringBuilder("On Error Resume Next \r\n\r\n");
            //表示本機
            sb.append("strComputer = \".\"  \r\n");
            //使用GetObject函數獲取本機信息賦值給objWMIService
            sb.append("Set objWMIService = GetObject(\"winmgmts:\" _ \r\n");
            sb.append("    & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n");
            sb.append("Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\")  \r\n ");
            //使用for循環取出CPU信息
            sb.append("For Each objItem in colItems\r\n " + "    Wscript.Echo objItem.ProcessorId  \r\n ");
            sb.append("    exit for  ' do the first cpu only! \r\n");
            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

  自定義簡易的機器碼,獻丑了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM