license主要用來機器的授權,加密。主要用到非對稱加密,散列加密,hex加密,還有機器碼的生成,一般機器碼包含:CPUID,主板序列號,硬盤序列號,MAC地址等;這裏主要包含了CPUID,主板序列號,硬盤序列號。
license主要用來防止客戶更換服務器硬件或者軟件破解等,主要流程就是使用非對稱加密內容防止破解,用唯一機器碼做服務器或者軟件綁定。
關於加密相關算法使用的hutool工具已經封裝好的算法,有特殊需要可以自己實現.
獲取CPUID,主板序列號,硬盤序列號的工具類:
public class SerialNumberUtil {
/**
* 獲取CPU-ID
*
* @return
*/
public static String getCPUId() {
String resultByCmd = getResultByCmd("sudo dmidecode -t 4 | grep ID");
return resultByCmd.split(":")[1].replace(" ", "");
}
/**
* 獲取主板序列號
*
* @return
*/
public static String getBiosSerial() {
String resultByCmd = getResultByCmd("sudo dmidecode -t 2 | grep Serial");
return resultByCmd.split(":")[1].replace(" ", "");
}
/**
* 獲取主板序列號
*
* @return
*/
public static List<String> getDiskSerial() {
List<String> lsblk = Lists.newArrayList(getResultByCmd("lsblk").split("\n"));
List<String> list = lsblk.stream().filter(va -> va.startsWith("sd")).collect(Collectors.toList());
List<String> stringList = list.stream().map(item ->
item.split(" ")[0].replace("[", "").replace("]", "")
).collect(Collectors.toList());
return stringList.stream().map(va -> {
//List<String> execForLines = RuntimeUtil.execForLines("sudo hdparm -i /dev/" + va);
List<String> execForLines = Lists.newArrayList(getResultByCmd("sudo hdparm -i /dev/" + va).split("\n"));
List<String> serialNo = execForLines.stream().filter(item -> item.contains("SerialNo")).collect(Collectors.toList());
String s = serialNo.get(0);
int start = s.indexOf("SerialNo=");
int end = s.length();
String substring = s.substring(start, end);
return substring.substring(substring.indexOf("=") + 1);
}).collect(Collectors.toList());
}
/**
* 執行命令獲取結果
*
* @param cmdStr
* @return
*/
public static String getResultByCmd(String cmdStr) {
try {
String[] cmd = new String[]{"/bin/sh", "-c", cmdStr};
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(ps.getInputStream()));
String line;
StringBuffer sb = new StringBuffer();
while ((line = b.readLine()) != null) {
sb.append(line).append("\n");
}
b.close();
return sb.toString();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return null;
}
}
}
生成機器碼,使用公鑰解密工具類:
@Slf4j
public class LicenseUtils {
/**
* DES加密祕鑰,DES需要16位的字符加密,所以祕鑰是8位,也可以自動補位
*/
private static final String password = "aaaaaaaa";
/**
* 獲取設備機器碼:主要是將獲取的硬件信息通過散列加密生成唯一的機器碼
*
* @return
*/
public static String getDeviceSN() {
DES des = SecureUtil.des(password.getBytes());
String cpuId = SerialNumberUtil.getCPUId();
String biosSerial = SerialNumberUtil.getBiosSerial();
List<String> diskSerial = SerialNumberUtil.getDiskSerial();
String str = cpuId + biosSerial + String.join("", diskSerial);
HMac hMac = new HMac(HmacAlgorithm.HmacMD5, des.getSecretKey());
return hMac.digestHex(str);
}
/**
* 獲取公鑰,公鑰密碼默認放在resource下面
*
* @return
*/
public static String getPublicKey() throws IOException {
ClassPathResource pathResource = new ClassPathResource("publicKey.txt");
InputStream inputStream = pathResource.getInputStream();
return new String(ByteStreams.toByteArray(inputStream));
}
/**
* 解密,先使用DES做HEX解密以後,再使用公鑰做非對稱解密
*
* @param cipherText
* @return
* @throws IOException
*/
public static String decrypt(String cipherText) {
String s;
try {
DES des = SecureUtil.des(password.getBytes());
RSA rsa = new RSA(null, getPublicKey());
byte[] decryptHex = des.decrypt(cipherText);
byte[] decrypt = rsa.decrypt(decryptHex, KeyType.PublicKey);
s = new String(decrypt);
log.info("decrypt res: " + s);
} catch (Exception e) {
log.error("decrypt error: " + e);
s = "0";
}
return s;
}
}
使用私鑰加密內容生成密文:
@Slf4j
public class LicenseUtils {
/**
* DES加密祕鑰,DES需要16位的字符
*/
private static final String password = "aaaaaaaa";
/**
* 獲取私鑰:私鑰存放在resource下面
*
* @return
*/
public static String getPrivateKey() throws IOException {
ClassPathResource pathResource = new ClassPathResource("privateKey.txt");
InputStream inputStream = pathResource.getInputStream();
return new String(ByteStreams.toByteArray(inputStream));
}
/**
* 加密:先使用私鑰加密以後再使用DES做HEX加密
*
* @param content
* @return
* @throws IOException
*/
public static String encrypt(String content) {
String s;
try {
DES des = SecureUtil.des(password.getBytes());
RSA rsa = new RSA(getPrivateKey(), null);
byte[] encrypt = rsa.encrypt(content, KeyType.PrivateKey);
s = des.encryptHex(encrypt);
log.info("encrypt res: " + s);
} catch (Exception e) {
log.error("encrypt error: " + e);
s = "0";
}
return s;
}
}