public class MachineCodeUtil {
public static final String LINUX_OS_NAME = "LINUX";
public static final String SYSTEM_PROPERTY_OS_NAME = "os.name";
public static void main(String[] args) throws IOException {
System.out.println(getThisMachineCode());
System.out.println(getCpuId());
System.out.println(getBiosUuid());
System.out.println("编码:"+encode("#######ssssss"));
System.out.println("解码:"+encode(getThisMachineCode(),"$$$$****"));
}
public static String getThisMachineCodeMd5(){
return encode(getThisMachineCode(),"$$$$****");
}
public static String encode(String data) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(data.getBytes(StandardCharsets.UTF_8));
byte[] bytes = messageDigest.digest();
return new BigInteger(1, bytes).toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
public static String encode(String data, String salt) {
return encode(data + salt);
}
public static String getThisMachineCode() {
try {
return getCpuId() + getBiosUuid();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String getCpuId() throws IOException {
String cpuId;
String os = System.getProperty(SYSTEM_PROPERTY_OS_NAME);
os = os.toUpperCase();
if (LINUX_OS_NAME.equals(os)) {
cpuId = getLinuxDmidecodeInfo("dmidecode -t processor | grep 'ID'", "ID", ":");
} else {
cpuId = getWindowsCpuId();
}
return cpuId.toUpperCase().replace(" ", "");
}
public static String getLinuxDmidecodeInfo(String cmd, String record, String symbol) throws IOException {
String execResult = executeLinuxCmd(cmd);
String[] infos = execResult.split("\n");
for (String info : infos) {
info = info.trim();
if (info.contains(record)) {
info.replace(" ", "");
String[] sn = info.split(symbol);
return sn[1];
}
}
return null;
}
public static String executeLinuxCmd(String cmd) throws IOException {
Runtime run = Runtime.getRuntime();
Process process;
process = run.exec(cmd);
InputStream processInputStream = process.getInputStream();
StringBuilder stringBuilder = new StringBuilder();
byte[] b = new byte[8192];
for (int n; (n = processInputStream.read(b)) != -1; ) {
stringBuilder.append(new String(b, 0, n));
}
processInputStream.close();
process.destroy();
return stringBuilder.toString();
}
public static String getWindowsCpuId() throws IOException {
Process process = Runtime.getRuntime().exec(
new String[]{
"wmic", "cpu", "get", "ProcessorId"});
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
sc.next();
String serial = sc.next();
return serial;
}
public static String getBiosUuid() throws IOException {
String cpuId;
String os = System.getProperty("os.name");
os = os.toUpperCase();
if ("LINUX".equals(os)) {
cpuId = getLinuxDmidecodeInfo("dmidecode -t system | grep 'UUID'", "UUID", ":");
} else {
cpuId = getWindowsBiosUUID();
}
return cpuId.toUpperCase().replace(" ", "");
}
public static String getWindowsBiosUUID() throws IOException {
Process process = Runtime.getRuntime().exec(
new String[]{
"wmic", "path", "win32_computersystemproduct", "get", "uuid"});
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
sc.next();
String serial = sc.next();
return serial;
}
}
文章评论