826220679@qq.com
2025-08-07 4d6cd980c5c69e4d9d150669c89734642297e0cd
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
package mac;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
 
public class SoftwareLicenseSystem {
    
    public static String usedata(String licenseKey, String BASE64_SECRET_KEY) {
        try {
            // Ö±½ÓÑéÖ¤´«ÈëµÄÐí¿ÉÖ¤ÃÜÔ¿
            if (validateLicense(licenseKey, BASE64_SECRET_KEY)) {
                Map<String, String> licenseInfo = parseLicense(licenseKey, BASE64_SECRET_KEY);
                return licenseInfo.get("expiry_date");
            } else {
                return "Èí¼þδÊÚȨ£¬Ç빺ÂòÊÚȨÂë";
            }
        } catch (Exception e) {
            return "ÊÚȨ¼ì²é´íÎó";
        }
    }
    
    // ÊÚȨ״̬¼ì²é
    public static boolean isok(String licenseKey, String BASE64_SECRET_KEY) {
        try {
            return validateLicense(licenseKey, BASE64_SECRET_KEY);
        } catch (Exception e) {
            return false;
        }
    }
    
    // ÑéÖ¤Ðí¿ÉÖ¤
    private static boolean validateLicense(String licenseKey, String BASE64_SECRET_KEY) throws Exception {
        if (licenseKey == null || licenseKey.isEmpty() || BASE64_SECRET_KEY == null || BASE64_SECRET_KEY.isEmpty()) {
            return false;
        }
        
        // ½âÃÜÊÚȨÂë
        String decryptedLicense = decrypt(Base64.getDecoder().decode(licenseKey), BASE64_SECRET_KEY);
        String[] parts = decryptedLicense.split("\\|");
        
        if (parts.length < 2) {
            return false; // ÎÞЧ¸ñʽ
        }
        
        // ÑéÖ¤»úÆ÷Âë
        String currentMachineCode = MachineCodeGenerator.getMachineCode();
        if (!currentMachineCode.equals(parts[0])) {
            System.err.println("ÊÚȨÂëÓ뵱ǰÉ豸²»Æ¥Åä");
            return false;
        }
        
        // ÑéÖ¤½ØÖ¹ÈÕÆÚ
        String expiryDateStr = parts[1];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date expiryDate = sdf.parse(expiryDateStr);
        Date currentDate = new Date();
        
        if (currentDate.after(expiryDate)) {
            System.err.println("Èí¼þÊÚȨÒѹýÆÚ");
            return false;
        }
        
        return true;
    }
    
    // ½âÎöÐí¿ÉÖ¤ÐÅÏ¢
    private static Map<String, String> parseLicense(String licenseKey, String BASE64_SECRET_KEY) throws Exception {
        String decryptedLicense = decrypt(Base64.getDecoder().decode(licenseKey), BASE64_SECRET_KEY);
        Map<String, String> licenseInfo = new HashMap<>();
        String[] parts = decryptedLicense.split("\\|");
        licenseInfo.put("machine_code", parts[0]);
        licenseInfo.put("expiry_date", parts[1]);
        return licenseInfo;
    }
    
    
    
    // »ñÈ¡ÃÜÔ¿×Ö½ÚÊý×飨±£³Ö²»±ä£©
    private static byte[] getSecretKeyBytes(String BASE64_SECRET_KEY) {
        return Base64.getDecoder().decode(BASE64_SECRET_KEY);
    }
    
    // AES½âÃÜ£¨±£³Ö²»±ä£©
    private static String decrypt(byte[] encryptedData, String BASE64_SECRET_KEY) throws Exception {
        byte[] keyBytes = getSecretKeyBytes(BASE64_SECRET_KEY);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedData);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
}