package com.lxzn.framework.utils; import lombok.extern.slf4j.Slf4j; import org.springframework.util.ResourceUtils; import java.io.*; import java.util.*; /** * @Description: * @Author: zhangherong * @Date: Created in 2021/6/17 8:56 * @Version: 1.0 * @Modified By: */ @Slf4j public class LicenseUtil { private static final String PREFIX = "N"; private static final String SUFFIX = "ENDn"; /** * 获取本地电脑的CPU序列号 * @return */ public static Set cpuSerialNumber(){ Scanner sc = null; try{ // Process process = Runtime.getRuntime().exec( // new String[] { "wmic", "cpu", "get", "ProcessorId" }); // process.getOutputStream().close(); Set set = new HashSet<>(); // sc = new Scanner(process.getInputStream()); // sc.next(); // while(sc.hasNext()){ // String serial = sc.next(); // set.add("178BFBFF00A40F41"); // } set.add("178BFBFF00A40F41"); return set; }catch (Exception e){ log.error("获取CPU序列号错误,{}", e); return null; }finally { if(sc != null){ sc.close(); } } } public static String generateLicense(Map map){ if(map.containsKey("cpu") && map.containsKey("D") && map.containsKey("M") && map.containsKey("R")) { StringBuilder sb = new StringBuilder(PREFIX); sb.append(map.get("cpu")); sb.append("D="); sb.append(map.get("D")); sb.append("M="); sb.append(map.get("M")); sb.append("R="); sb.append(map.get("R")); sb.append(SUFFIX); return DESUtil.encrypt(DESUtil.key, sb.toString()); } return null; } public static Map parseLicense(String license){ license = DESUtil.decrypt(DESUtil.key, license); if(license != null){ license = license.substring(1); license = license.substring(0, license.length()-4); //System.out.println(license); String[] arr = license.split("="); if(arr.length == 4){ Map map = new HashMap<>(4); map.put("cpu", arr[0].substring(0, arr[0].length() - 1)); map.put("D", arr[1].substring(0, arr[1].length() - 1)); map.put("M", arr[2].substring(0, arr[2].length() - 1)); map.put("R", arr[3]); return map; } } return null; } public static String readLicense(String path){ BufferedReader br = null; try{ //File file = ResourceUtils.getFile("classpath:license.txt"); File file = ResourceUtils.getFile(path); InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"UTF-8"); br = new BufferedReader(reader); return br.readLine(); }catch (Exception e){ log.error("读取序列号错误,{}", e); return null; }finally { if(br != null){ try { br.close(); } catch (IOException e) { } } } } public static void main(String[] args) { //System.out.println(cpuSerialNumber()); // Map map = new HashMap<>(4); // map.put("cpu", cpuSerialNumber()); // map.put("D", "10"); // map.put("M", "10"); // map.put("R", "2022-07-26"); // String license = generateLicense(map); // System.out.println(license); // System.out.println(DESUtil.decrypt(DESUtil.key, license)); String license = "khosTphea6sZhkLLaHuSOjUC63bNZ37IuOfHQKdC72Yxg8Lh2HWT9FLzEsZl8MD1"; Map stringStringMap = parseLicense(license); System.out.println(stringStringMap); // String s = readLicense(); // System.out.println(s); } }