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