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
自定义简易的机器码,献丑了。