lyh
15 小时以前 78aeb8a8c97a884a640d46755e4be706bde48b7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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<String> cpuSerialNumber(){
        Scanner sc = null;
        try{
//            Process process = Runtime.getRuntime().exec(
//                    new String[] { "wmic", "cpu", "get", "ProcessorId" });
//            process.getOutputStream().close();
            Set<String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> stringStringMap = parseLicense(license);
        System.out.println(stringStringMap);
 
//        String s = readLicense();
//        System.out.println(s);
    }
}