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
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();
    }
}