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