package com.billion.main.licenes; import com.billion.main.licenes.HardwareUtils; import com.billion.main.licenes.LicenseVerifier; import java.nio.file.Files; import java.nio.file.Paths; import java.security.MessageDigest; import java.text.SimpleDateFormat; import java.util.Date; public class LicenseGenerator { private static final String SECRET_KEY = "SgdDUyWs+ZPIaQr4oj7D2T9ISM9eu66cLulRCuL283k="; // 通过SecretKeyGenerator生成 /** * 生成授权文件 * @param expireDate 过期时间 格式:yyyy-MM-dd */ public static void generateLicense(String expireDate) throws Exception { // 获取当前机器的硬件信息 String machineId = getMachineId(); System.out.println("当前机器ID: " + machineId); // 生成授权内容 String content = machineId + "|" + expireDate; String signature = generateSignature(content); // 组合完整的授权信息 String license = content + "|" + signature; // 保存到文件 Files.write(Paths.get("license.dat"), license.getBytes()); System.out.println("授权文件已生成: license.dat"); // 保存机器ID(用于后续验证) Files.write(Paths.get("machine.id"), machineId.getBytes()); System.out.println("机器ID文件已生成: machine.id"); } /** * 获取机器ID(组合多个硬件信息) */ private static String getMachineId() throws Exception { StringBuilder machineId = new StringBuilder(); // 获取CPU信息 String cpuId = HardwareUtils.getCPUID(); machineId.append(cpuId).append("#"); // 获取主板序列号 String motherboardSN = HardwareUtils.getMotherboardSN(); machineId.append(motherboardSN).append("#"); // 获取硬盘序列号 String diskSN = HardwareUtils.getDiskSN(); machineId.append(diskSN).append("#"); // 获取MAC地址(可选) String macAddress = getFirstMacAddress(); machineId.append(macAddress); // 对组合的信息进行SHA256加密 return sha256(machineId.toString()); } /** * 获取第一个网卡的MAC地址 */ private static String getFirstMacAddress() { try { java.util.Enumeration interfaces = java.net.NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { java.net.NetworkInterface network = interfaces.nextElement(); byte[] mac = network.getHardwareAddress(); if (mac != null && mac.length > 0) { StringBuilder sb = new StringBuilder(); for (byte b : mac) { sb.append(String.format("%02X", b)); } return sb.toString(); } } } catch (Exception e) { e.printStackTrace(); } return "UNKNOWN_MAC"; } /** * 生成签名 */ private static String generateSignature(String content) throws Exception { return sha256(content + SECRET_KEY); } /** * SHA256加密 */ 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(); } /** * 使用示例 */ public static void main(String[] args) { try { // 设置过期时间为一年后 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date twoHoursLater = new Date(System.currentTimeMillis() + 2L * 60 * 60 * 1000); String expireDate = sdf.format(twoHoursLater); // 生成授权文件 generateLicense(expireDate); // 验证生成的授权文件 boolean isValid = LicenseVerifier.verify(); System.out.println("授权验证结果: " + isValid); } catch (Exception e) { e.printStackTrace(); } } }