whycxzp
2022-05-17 0a206fc6c66c564aced22ad6b16f6f8be27e2fc7
更新加密解密
5个文件已修改
1个文件已添加
216 ■■■■ 已修改文件
src/main/java/com/whyc/App.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ComInfoController.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/filter/CrossDomainFilter.java 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/pojo/ComInfo.java 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/pojo/HardWareUtils.java 120 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ComInfoService.java 39 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/App.java
@@ -3,12 +3,14 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableWebMvc
@ServletComponentScan(basePackages = {"com.whyc.filter"})
public class App extends WebMvcConfigurerAdapter implements WebMvcConfigurer {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
src/main/java/com/whyc/controller/ComInfoController.java
@@ -20,9 +20,10 @@
    public Response getComInfoRSA(){
        return service.getComInfoRSA();
    }
    @ApiOperation("获取电脑cpu的ID和主板的序列号(rsa解密后的)")
    @PostMapping("getComInfo")
    public Response getComInfo(@RequestBody ComInfo cInfo){
        return service.getComInfo(cInfo);
    @GetMapping("getComInfo")
    public Response getComInfo(@RequestParam String serialNumberMixRSA){
        return service.getComInfo(serialNumberMixRSA);
    }
}
src/main/java/com/whyc/filter/CrossDomainFilter.java
New file
@@ -0,0 +1,40 @@
package com.whyc.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Description :
 * @date 2020/09/11
 **/
@WebFilter
public class CrossDomainFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("执行了过滤器CrossDomainFilter");
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp= (HttpServletResponse) response;
        HttpServletRequest req= (HttpServletRequest) request;
        String origin = req.getHeader("Origin");
        // String origin = "http://localhost:8080";
        resp.setHeader("Access-Control-Allow-Origin", origin);
        resp.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
        resp.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        chain.doFilter(request, resp);
    }
    @Override
    public void destroy() {
    }
}
src/main/java/com/whyc/pojo/ComInfo.java
@@ -17,10 +17,10 @@
public class ComInfo {
    @ApiModelProperty("cpu的Id")
    private String cpuId;
    @ApiModelProperty("Rsa加密后cpu的Id")
    private String cpuIdRsa;
    /*@ApiModelProperty("Rsa加密后cpu的Id")
    private String cpuIdRsa;*/
    @ApiModelProperty("主板序列号")
    private String boardId;
    @ApiModelProperty("Rsa加密后主板序列号")
    private String boardIdRsa;
    /*@ApiModelProperty("Rsa加密后主板序列号")
    private String boardIdRsa;*/
}
src/main/java/com/whyc/pojo/HardWareUtils.java
@@ -3,12 +3,10 @@
import javax.annotation.Resource;
import java.applet.Applet;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Scanner;
public class HardWareUtils extends Applet {
    public HardWareUtils(){
@@ -156,14 +154,120 @@
    }
    /**linux*/
    public static String executeLinuxCmd(String cmd)  {
        try {
            System.out.println("got cmd job : " + cmd);
            Runtime run = Runtime.getRuntime();
            Process process;
            process = run.exec(cmd);
            InputStream in = process.getInputStream();
            BufferedReader bs = new BufferedReader(new InputStreamReader(in));
            StringBuffer out = new StringBuffer();
            byte[] b = new byte[8192];
            for (int n; (n = in.read(b)) != -1;) {
                out.append(new String(b, 0, n));
            }
            in.close();
            process.destroy();
            return out.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     *
     * @param cmd 命令语句
     * @param record 要查看的字段
     * @param symbol 分隔符
     * @return
     */
    public static String getSerialNumber(String cmd ,String record,String symbol) {
        String execResult = executeLinuxCmd(cmd);
        String[] infos = execResult.split("\n");
        for(String info : infos) {
            info = info.trim();
            if(info.indexOf(record) != -1) {
                info.replace(" ", "");
                String[] sn = info.split(symbol);
                return sn[1];
            }
        }
        return null;
    }
    /**
     * CPU序列号-系统兼容
     * @return
     */
    public static String getCPUNumber(){
        String os = System.getProperty("os.name").toLowerCase();
        Process process = null;
        String serial = null;
        if(os.contains("window")) {
            try {
                process = Runtime.getRuntime().exec(
                        new String[]{"wmic", "cpu", "get", "ProcessorId"});
                process.getOutputStream().close();
                Scanner sc = new Scanner(process.getInputStream());
                String property = sc.next();
                serial = sc.next();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            //linux
            serial =getSerialNumber("dmidecode -t processor | grep 'ID'", "ID",":");
        }
        return serial;
    }
    /**
     * 主板序列号-系统兼容
     */
    public static String getBaseboardNumber(){
        String os = System.getProperty("os.name").toLowerCase();
        Process process = null;
        String serial = null;
        if(os.contains("window")) {
            try {
                process = Runtime.getRuntime().exec(
                        new String[]{"wmic", "baseboard", "get", "serialnumber"});
                process.getOutputStream().close();
                Scanner sc = new Scanner(process.getInputStream());
                String property = sc.next();
                serial = sc.next();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            //linux
            serial =getSerialNumber("dmidecode |grep 'Serial Number'", "Serial Number",":");
        }
        return serial;
    }
    public static void main(String[] args) throws Exception {
        System.out.println(getCPUSerial());//CPU
        System.out.println(getMotherboardSN());//主板
        System.out.println(getHardDiskSN("c"));//c盘
        System.out.println(getMac());//MAC
        String msg  = getCPUSerial()+getMotherboardSN().replace(".", "")+getHardDiskSN("c")+getMac().replace("-", "");
        System.out.println("原始数据:"+msg);
        System.out.println(getBaseboardNumber());
    }
src/main/java/com/whyc/service/ComInfoService.java
@@ -5,24 +5,39 @@
import com.whyc.pojo.RSAUtil;
import com.whyc.pojo.Response;
import org.springframework.stereotype.Service;
import sun.security.mscapi.CKeyPairGenerator;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
@Service
public class ComInfoService {
    public Response getComInfoRSA(){
        ComInfo cInfo=new ComInfo();
        cInfo.setCpuId(HardWareUtils.getCPUSerial());
        cInfo.setCpuIdRsa(RSAUtil.encrypt(cInfo.getCpuId(),RSAUtil.getPublicKey()));
        cInfo.setBoardId(HardWareUtils.getMotherboardSN());
        cInfo.setBoardIdRsa(RSAUtil.encrypt(cInfo.getBoardId(),RSAUtil.getPublicKey()));
        return  new Response().setII(1,cInfo,cInfo!=null?true:false,"");
        try {
            String cpuNumber = HardWareUtils.getCPUNumber();
            String baseboardNumber = HardWareUtils.getBaseboardNumber();
            String serialNumberMix = cpuNumber + RSAUtil.fontSeparator + baseboardNumber;
            String serialNumberMixRSA = RSAUtil.encrypt(serialNumberMix, RSAUtil.getPublicKey());
            return  new Response().set(1,serialNumberMixRSA);
        }catch (Exception e){
            return new Response().set(0);
        }
    }
    public Response getComInfo(ComInfo cInfo) {
        cInfo.setCpuId(RSAUtil.decrypt(cInfo.getCpuIdRsa(),RSAUtil.getPrivateKey()));
        cInfo.setBoardId(RSAUtil.decrypt(cInfo.getBoardIdRsa(),RSAUtil.getPrivateKey()));
        return  new Response().setII(1,cInfo,cInfo!=null?true:false,"");
    public Response getComInfo(String  serialNumberMixRSA) {
        try {
            serialNumberMixRSA = URLDecoder.decode(serialNumberMixRSA, "utf-8");
            String serialNumberMix = RSAUtil.decrypt(serialNumberMixRSA, RSAUtil.getPrivateKey());
            String[] numberArr = serialNumberMix.split(RSAUtil.fontSeparator);
            ComInfo comInfo = new ComInfo();
            comInfo.setCpuId(numberArr[0]);
            comInfo.setBoardId(numberArr[1]);
            return new Response().set(1,comInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Response().set(0);
    }
}