whyczh
2021-12-10 1ab06fa644b400182dde1621c60f904c4711f2b6
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
package com.whyc.service;
 
import com.whyc.dto.Response;
import com.whyc.encryption.ByteConvertUtil;
import com.whyc.encryption.SM2;
import org.springframework.stereotype.Service;
 
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
 
@Service
public class LicenseService {
 
    /**
     * 证书文件校验,检查有效性并将文件存储在数据库
     */
    public Response checkLicense(String license){
        Response model = new Response();
        boolean res = true;
        String origin = "Company: Fuguang Electronic\n"
                + "Project:BTS monitor platform\n"
                + "Licence type:";
 
        //初始化sm2参数x
        SM2 x = new SM2();
//        String realPath = ServletActionContext.getServletContext().getRealPath("/");
//        BigInteger privKey = x.importPrivateKey(realPath+"WEB-INF/classes/pri_key.ksm");
        BigInteger privKey = x.importPrivateKey("WEB-INF/classes/pri_key.ksm");
 
 
        //解密
        try {
            byte[] bytes = ByteConvertUtil.hexToByteArray(license);
            String decryptResult = x.decrypt(bytes, privKey);
 
            if(decryptResult.indexOf(origin)!=-1){
                String[] split = decryptResult.split("Valid time:");
                if(split.length>1){
                    String validTime=split[1];
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String today = dateFormat.format(new Date());
                    if(validTime.compareTo(today)<0){
                        res=false;
                    }
                }
            }else{
                res=false;
            }
 
        }catch (Exception e){
            res=false;
            e.printStackTrace();
        }
        //license校验通过,校验数据库中是否已经存在
        if(res){
//            res =!(new User_infImpl().licenseExist(license));
        }
        if(res){
            model.setCode(1);
        }else{
            model.setMsg("无效的license");
        }
        return model;
    }
 
 
 
}