From 49ea842aa493a2ada79d6833f4ff85fc8291c0cc Mon Sep 17 00:00:00 2001 From: whycxzp <perryhsu@163.com> Date: 星期一, 14 三月 2022 10:49:16 +0800 Subject: [PATCH] SM2的公私钥获取方式更新 --- src/main/java/com/whyc/controller/LicenseController.java | 46 +++++++++++++++++++++------------------------- 1 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/whyc/controller/LicenseController.java b/src/main/java/com/whyc/controller/LicenseController.java index ff5aa41..cf234d7 100644 --- a/src/main/java/com/whyc/controller/LicenseController.java +++ b/src/main/java/com/whyc/controller/LicenseController.java @@ -11,18 +11,13 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.bouncycastle.math.ec.ECPoint; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.system.ApplicationHome; -import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; import org.springframework.util.ClassUtils; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.math.BigInteger; @RequestMapping("license") @@ -31,9 +26,12 @@ public class LicenseController { @Resource private LicenseService service; + /** + * 绫诲姞杞芥椂鍒濆鍖杝m2鐨勫叕绉侀挜 + */ + final static ECPoint publicKey = SM2.getPublicKey(); + final static BigInteger privateKey = SM2.getPrivateKey(); - @Autowired - private Environment environment; /** * 妫�楠屾湇鍔″櫒鏄惁娉ㄥ唽,鏄惁宸插瓨鍦ㄥ簭鍒楀彿 @@ -63,7 +61,7 @@ @ApiOperation(value = "鑾峰彇搴忓垪鍙穕icense") public Response getSerialNumberLicense(){ //搴忓垪鍙峰姞瀵� license - Response model=createLicense(System.currentTimeMillis()+"createTime"+ SerialNumberUtil.getSerialNumber()); + Response model=LicenseController.createLicense(System.currentTimeMillis()+"createTime"+ SerialNumberUtil.getSerialNumber()); //鍚屾椂,灏嗗簭鍒楀彿鐢熸垚鏃堕棿璁板綍鍒癮pplication鍩熶腑 //getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis()); //ActionUtil.getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis()); @@ -71,7 +69,7 @@ } //鑾峰彇涓�涓猯icense - public Response createLicense(String serialNumber){ + public static Response createLicense(String serialNumber){ //鍒濆鍖杝m2鍙傛暟x SM2 x = new SM2(); /*String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath(); @@ -79,15 +77,17 @@ BigInteger privKey = x.importPrivateKey(realPath+"config/pri_key.ksm"); ECPoint pubKey = x.importPublicKey(realPath+"config/pub_key.ksm");*/ - String fileDirName=getRealPath("pub_key.ksm"); - ECPoint pubKey = x.importPublicKey(fileDirName+"/pub_key.ksm"); + //鏃х増鏈� + /*String fileDirName=LicenseController.getRealPath("pub_key.ksm"); + ECPoint pubKey = x.importPublicKey(fileDirName+"/pub_key.ksm");*/ //System.out.println("pubKey "+pubKey); /*String origin = "Company: Fuguang Electronic\n" + "Project:BTS monitor platform\n" + "Licence type:Permanent";*/ //鑾峰彇鍔犲瘑鍒楄〃 //System.out.println("origin "+origin); - byte[] encryptResult = x.encrypt(serialNumber, pubKey); + //byte[] encryptResult = x.encrypt(serialNumber, pubKey); + byte[] encryptResult = x.encrypt(serialNumber, publicKey); String encrypt = ByteConvertUtil.bytesToHexString(encryptResult); //System.out.println("encrypt:"+encrypt); return new Response().set(1,encrypt); @@ -108,15 +108,16 @@ SM2 x = new SM2(); //String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath(); //ECPoint pubKey = x.importPublicKey(realPath + "config/pub_key.ksm"); - String fileDirName=getRealPath("pri_key.ksm"); - BigInteger privKey = x.importPrivateKey(fileDirName + "/pri_key.ksm"); + /*//鏃х増鏈� + String fileDirName=LicenseController.getRealPath("pri_key.ksm"); + BigInteger privKey = x.importPrivateKey(fileDirName + "/pri_key.ksm");*/ String origin = "Company: Fuguang Electronic\n" + "Project:BTS monitor platform\n" + "Licence duration:"; //鑾峰彇瑙e瘑鍚巐icense,闄勫甫鏍¢獙license缂栫爜鏍煎紡 String decryptResult = null; byte[] bytes = ByteConvertUtil.hexToByteArray(license); - decryptResult = x.decrypt(bytes, privKey); + decryptResult = x.decrypt(bytes, privateKey); //鐢ㄦ埛鍙兘寰�灏忚皟鏃堕棿 String[] split1 = decryptResult.split("machineCode:"); Long registerCodeTime = Long.valueOf(split1[0]); @@ -175,18 +176,13 @@ //灏唒ri_key.ksm銆俻ub_key.ksm鏂囦欢鎷疯礉鑷砶sm鏂囦欢涓嬬劧鍚庤鍙杅ileName:/config/pri_key.ksm - public String getRealPath(String fileName){ + public static String getRealPath(String fileName){ ClassPathResource classPathResource = new ClassPathResource("/config/"+fileName); InputStream inputStream_pub = null; ApplicationHome applicationHome = new ApplicationHome(LicenseController.class); File jarFile = applicationHome.getDir(); - int configType = Integer.parseInt(environment.getProperty("configFile.type")); - String fileDirName = ""; - if(configType==1){ - fileDirName = jarFile.getParentFile().toString()+ File.separator+"ksm"; - }else { - fileDirName = jarFile.toString()+File.separator+"ksm";//鎵撳寘鐗堟湰 - } + String fileDirName = jarFile.getParentFile().toString()+ File.separator+"ksm"; + //String fileDirName = jarFile.toString()+File.separator+"ksm";//鎵撳寘鐗堟湰 createFile(fileDirName);//鍒涘缓鏂囦欢澶筴sm try { inputStream_pub = classPathResource.getInputStream(); @@ -203,7 +199,7 @@ } return fileDirName; } - public void createFile(String pathName) { + public static void createFile(String pathName) { File dir = new File(pathName); if (!dir.exists()) {// 鍒ゆ柇鐩綍鏄惁瀛樺湪 dir.mkdir(); -- Gitblit v1.9.1