public class LicenceCheckout {
private String hexByte(byte b) {
String s = "000000" + Integer.toHexString(b);
return s.substring(s.length() - 2);
}
private String getSystemCode() {
try {
Enumeration<NetworkInterface> el = NetworkInterface.getNetworkInterfaces();
while (el.hasMoreElements()) {
NetworkInterface ni = el.nextElement();
byte[] mac = ni.getHardwareAddress();
if (null != mac && mac.length > 0) {
StringBuilder builder = new StringBuilder();
for (byte b : mac)
builder.append(hexByte(b));
return builder.toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String encode() {
try {
byte[] systemCode = getSystemCode().getBytes("UTF8");
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(systemCode);
String result = "";
byte[] temp;
temp = md5.digest(systemCode);
for (int i = 0; i < temp.length; i++) {
result += Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String getLicence() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("licence")));
String line1 = br.readLine();
return line1;
}
public boolean hasAuthorized() {
try {
// 比较两个加密文件是否一致
if (encode().equals(getLicence().trim()))
return true;
System.out.println("授权失败!!!");
} catch (Exception e) {
System.out.println("授权验证失败,原因:" + e.getMessage());
}
return false;
}
public static void main(String[] args) throws SocketException {
LicenceCheckout t = new LicenceCheckout();
System.out.println(t.hasAuthorized());
}
}