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