admin
2025-03-29 9cd0e111d7315074f5574558dab3cae087ff1f68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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<java.net.NetworkInterface> 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();
        }
    }
}