package com.billion.main.licenes;
|
|
import java.nio.file.Files;
|
import java.nio.file.Paths;
|
import java.security.MessageDigest;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
|
public class LicenseVerifier {
|
private static final String LICENSE_FILE = "license.dat";
|
private static final String MACHINE_ID_FILE = "machine.id";
|
|
public static boolean verify() {
|
try {
|
// 检查授权文件是否存在
|
if (!Files.exists(Paths.get(LICENSE_FILE))) {
|
System.out.println("未找到授权文件!");
|
return false;
|
}
|
|
// 读取授权文件
|
String license = new String(Files.readAllBytes(Paths.get(LICENSE_FILE)));
|
String[] parts = license.split("\\|");
|
if (parts.length != 3) {
|
return false;
|
}
|
|
// 解析授权信息
|
String machineId = parts[0];
|
String expireDate = parts[1];
|
String signature = parts[2];
|
|
// 验证机器码
|
if (!validateMachineId(machineId)) {
|
System.out.println("机器码验证失败!");
|
return false;
|
}
|
|
// 验证过期时间
|
if (!validateExpireDate(expireDate)) {
|
System.out.println("授权已过期!");
|
return false;
|
}
|
|
// 验证签名
|
if (!validateSignature(machineId + "|" + expireDate, signature)) {
|
System.out.println("签名验证失败!");
|
return false;
|
}
|
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return false;
|
}
|
}
|
|
private static boolean validateMachineId(String licenseMachineId) {
|
try {
|
// 获取当前机器的硬件信息
|
String currentMachineId = getMachineId();
|
return licenseMachineId.equals(currentMachineId);
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
|
private static String getMachineId() throws Exception {
|
// 获取机器硬件信息(示例使用主板序列号和CPU序列号)
|
String motherboardSn = executeCommand("/DNZW8T3/CNCMC002A70151/");
|
String cpuId = executeCommand("BFEBFBFF000906A3");
|
|
// 组合并加密机器码
|
String machineId = motherboardSn + cpuId;
|
return sha256(machineId);
|
}
|
|
private static String executeCommand(String command) throws Exception {
|
Process process = Runtime.getRuntime().exec(command);
|
java.io.BufferedReader reader = new java.io.BufferedReader(
|
new java.io.InputStreamReader(process.getInputStream()));
|
String line;
|
StringBuilder sb = new StringBuilder();
|
while ((line = reader.readLine()) != null) {
|
sb.append(line);
|
}
|
return sb.toString().replaceAll("\\s+", "");
|
}
|
|
private static boolean validateExpireDate(String expireDate) {
|
try {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
Date expire = sdf.parse(expireDate);
|
return new Date().before(expire);
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
|
private static boolean validateSignature(String content, String signature) {
|
try {
|
String calculatedSignature = sha256(content + "SgdDUyWs+ZPIaQr4oj7D2T9ISM9eu66cLulRCuL283k=");
|
return signature.equals(calculatedSignature);
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
|
private static String sha256(String input) throws Exception {
|
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
byte[] hash = digest.digest(input.getBytes("UTF-8"));
|
StringBuilder hexString = new StringBuilder();
|
for (byte b : hash) {
|
String hex = Integer.toHexString(0xff & b);
|
if (hex.length() == 1) hexString.append('0');
|
hexString.append(hex);
|
}
|
return hexString.toString();
|
}
|
}
|