From 4048694d00aed4a294d1f870828164f79946e13f Mon Sep 17 00:00:00 2001 From: stone <827672943@qq.com> Date: Wed, 20 Mar 2024 15:20:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=A4=E8=AF=81=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dntd-common/pom.xml | 7 +- dntd-model-buildheating/pom.xml | 7 +- dntd-model-heatboiler/pom.xml | 7 +- dntd-modelI/pom.xml | 7 +- dntd-tool/pom.xml | 7 +- .../main/java/com/dky/security/SM4Utils.java | 78 +++++------ .../src/main/java/com/dky/tool/ModelTool.java | 128 +++++++++++------- file.txt | 3 - pom.xml | 41 +++--- 9 files changed, 146 insertions(+), 139 deletions(-) delete mode 100644 file.txt diff --git a/dntd-common/pom.xml b/dntd-common/pom.xml index a2857de..e127269 100644 --- a/dntd-common/pom.xml +++ b/dntd-common/pom.xml @@ -10,7 +10,7 @@ dntd-common - 1.1-SNAPSHOT + 1.0-SNAPSHOT 8 @@ -22,7 +22,10 @@ cn.hutool hutool-all - 5.4.5 + + + org.bouncycastle + bcprov-jdk15to18 diff --git a/dntd-model-buildheating/pom.xml b/dntd-model-buildheating/pom.xml index 79fcc8d..0183ac9 100644 --- a/dntd-model-buildheating/pom.xml +++ b/dntd-model-buildheating/pom.xml @@ -10,7 +10,7 @@ dntd-model-buildheating - 1.1-SNAPSHOT + 1.0-SNAPSHOT 8 @@ -19,11 +19,6 @@ - - cn.hutool - hutool-all - 5.4.5 - com.dky dntd-modelI diff --git a/dntd-model-heatboiler/pom.xml b/dntd-model-heatboiler/pom.xml index 8ac61dd..c896c15 100644 --- a/dntd-model-heatboiler/pom.xml +++ b/dntd-model-heatboiler/pom.xml @@ -10,7 +10,7 @@ dntd-model-heatboiler - 1.1-SNAPSHOT + 1.0-SNAPSHOT 8 @@ -19,11 +19,6 @@ - - cn.hutool - hutool-all - 5.4.5 - com.dky dntd-modelI diff --git a/dntd-modelI/pom.xml b/dntd-modelI/pom.xml index c41a522..b04330a 100644 --- a/dntd-modelI/pom.xml +++ b/dntd-modelI/pom.xml @@ -10,7 +10,7 @@ dntd-modelI - 1.1-SNAPSHOT + 1.0-SNAPSHOT 8 @@ -18,11 +18,6 @@ UTF-8 - - cn.hutool - hutool-all - 5.4.5 - com.dky diff --git a/dntd-tool/pom.xml b/dntd-tool/pom.xml index fe2bd98..3637158 100644 --- a/dntd-tool/pom.xml +++ b/dntd-tool/pom.xml @@ -10,7 +10,7 @@ dntd-tool - 1.2-SNAPSHOT + 1.0-SNAPSHOT 8 @@ -18,11 +18,6 @@ UTF-8 - - cn.hutool - hutool-all - 5.4.5 - org.bouncycastle bcprov-jdk15on diff --git a/dntd-tool/src/main/java/com/dky/security/SM4Utils.java b/dntd-tool/src/main/java/com/dky/security/SM4Utils.java index 72f9d69..c5586f2 100644 --- a/dntd-tool/src/main/java/com/dky/security/SM4Utils.java +++ b/dntd-tool/src/main/java/com/dky/security/SM4Utils.java @@ -1,12 +1,13 @@ package com.dky.security; +import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.asymmetric.KeyType; +import cn.hutool.crypto.asymmetric.SM2; import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; +import java.io.*; import java.nio.charset.StandardCharsets; import java.security.Security; import java.util.*; @@ -16,7 +17,8 @@ public class SM4Utils { Security.addProvider(new BouncyCastleProvider()); } - private static final String SM4_KEY = "mxhXSDiPYFjYgzRb"; // 16 bytes key + public final static String SM4_KEY = "1100fba8ee67ddf1f6f4e37c500dc10eee1bf15827ae3837810e30f402fa0bc6"; + private static final String ALGORITHM = "AES/ECB/PKCS5Padding"; public static String encrypt(Map map) throws Exception { @@ -28,29 +30,33 @@ public class SM4Utils { return Base64.getEncoder().encodeToString(encrypted); } - public static List> decrypt(String encrypted) throws Exception { - List> ary = new ArrayList<>(); + public static Map decrypt(String encrypted) throws FileNotFoundException { File file = new File("file.txt"); FileReader fileReader = new FileReader(file); - BufferedReader reader = new BufferedReader(fileReader); - String line; - while ((line = reader.readLine()) != null) { - Cipher cipher = Cipher.getInstance(ALGORITHM, "BC"); - SecretKeySpec secretKey = new SecretKeySpec(line.getBytes(StandardCharsets.UTF_8), "AES"); - cipher.init(Cipher.DECRYPT_MODE, secretKey); - byte[] decoded = Base64.getDecoder().decode(encrypted); - try { - byte[] decrypted = cipher.doFinal(decoded); - ary.add(stringToMap(new String(decrypted, StandardCharsets.UTF_8))); - } catch (Exception e) { - // e.printStackTrace(); + try (BufferedReader reader = new BufferedReader(fileReader)) { + String line; + while ((line = reader.readLine()) != null) { + try { + Cipher cipher = Cipher.getInstance(ALGORITHM, "BC"); + SecretKeySpec secretKey = new SecretKeySpec(line.getBytes(StandardCharsets.UTF_8), "AES"); + cipher.init(Cipher.DECRYPT_MODE, secretKey); + byte[] decoded = Base64.getDecoder().decode(encrypted); + byte[] decrypted = cipher.doFinal(decoded); + return stringToMap(new String(decrypted, StandardCharsets.UTF_8)); + } catch (Exception e) { + System.out.println("私钥:" + line + ",该私钥未认证"); + e.printStackTrace(); + } } + } catch ( IOException e){ + System.out.println("文件读取错误,请检查文件内容是否为空"); + throw new RuntimeException(); } - - return ary; + return null; } + private static String mapToString(Map map) { StringBuilder sb = new StringBuilder(); for (Map.Entry entry : map.entrySet()) { @@ -71,28 +77,16 @@ public class SM4Utils { return map; } - public static void main(String[] args) throws Exception { - String s = - "GWnQ4RqqTc8n1Uj59xLoUtv975fmQsRWuvsk1zRmQu9TwIvlc6FTekndKMh+vMuRbI2bxdmuIxyZndYcg9u5xVa+HaiBZRP8OZFYIAo+66vDVlkBf47Nh2srjFyIXlLH"; - List> decryptList = decrypt(s); - decryptList.forEach(System.out::println); - - /* - File file = new File("file.txt"); - try { - FileReader fileReader = new FileReader(file); - BufferedReader reader = new BufferedReader(fileReader); - String line; - while ((line = reader.readLine()) != null) { - // 处理每一行的数据 - System.out.println(line); - } - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - }*/ - - + /** + * SM2私钥解密 + * + * @param encryptStr SM2加密字符串 + * @return + */ + public static String sm2DecryptBase64(String encryptStr) { + SM2 sm2 = new SM2(SM4_KEY, null); + return StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey)); } + } \ No newline at end of file diff --git a/dntd-tool/src/main/java/com/dky/tool/ModelTool.java b/dntd-tool/src/main/java/com/dky/tool/ModelTool.java index 960052d..c548c3a 100644 --- a/dntd-tool/src/main/java/com/dky/tool/ModelTool.java +++ b/dntd-tool/src/main/java/com/dky/tool/ModelTool.java @@ -3,19 +3,15 @@ package com.dky.tool; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import com.dky.modelI.DntdModelI; -import com.dky.security.GetCpuInfo; import com.dky.utils.ConfigReader; import com.dky.utils.entity.SysDeviceHeatScene; import com.dky.utils.enums.Code; import com.dky.utils.result.ResponseUtil; -import org.bouncycastle.math.raw.Mod; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; +import java.util.*; public class ModelTool { @@ -41,58 +37,90 @@ public class ModelTool { } - public JSONObject exeModel2Report(JSONObject jsonObject, String key) { + public JSONObject exeModel2Report(JSONObject jsonObject, String key,List mapList) { JSONObject jsonObjectResult = new JSONObject(); // 判断CpuId是否包含本机地址 - Boolean thisCpuIdIn = false; - // 判断是否过期 - Boolean expired = false; - + Boolean isAuthorization = Boolean.FALSE; + // 参数合法性检查 + if (key == null || key.isEmpty()) { + System.err.println("解密密钥不能为空"); + return ResponseUtil.createResponse(Code.KEY_CONTEXT_ERROR.getCode(), Code.KEY_CONTEXT_ERROR.getDesc(), null); + } + //这里会去查询两张表,分别是mysql中的information_schema库的tables和statistics表中的设备数据表元信息,其中有用的只是tables中的create_time字段,其余字段没有任何意义,只是为了防止破解 + if (mapList.size() != 2){ + System.err.println("未获取到验证信息"); + return ResponseUtil.createResponse(Code.KEY_CONTEXT_ERROR.getCode(), Code.KEY_CONTEXT_ERROR.getDesc(), null); + } try { - //获取本地的cpuid. - String localCpuId = GetCpuInfo.getCpuId(); // 根据给定的类名初始化类 加密不需要反实例化new - Class sm4UtilsClass = Class.forName("com.dky.security.SM4Utils"); - // 实例化这个类 - Object obj = sm4UtilsClass.newInstance(); - // 调用指定方法 - List maps = (List) sm4UtilsClass.getMethod("decrypt", String.class).invoke(obj, key); - for (Map map : maps){ - String cpuIds = (String) map.get("cpuIds"); - String expireTime = (String) map.get("expireTime"); - // 定义日期时间格式 - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date date = formatter.parse(expireTime); - if (cpuIds.contains(localCpuId)){ - thisCpuIdIn = true; - } - if (new Date().before(date)){ - expired = true; - } + Class sm4UtilsClass = Class.forName("com.dky.security.SM4Utils"); + // 实例化类 + Object obj = sm4UtilsClass.getDeclaredConstructor().newInstance(); + // 获取方法并调用 + Method decryptMethod = sm4UtilsClass.getMethod("sm2DecryptBase64", String.class); + String invoke = (String) decryptMethod.invoke(obj, key); + Map map = new HashMap<>(); + String[] keyValuePairs = invoke.split("&"); + for (String keyValuePair : keyValuePairs) { + String[] keyValue = keyValuePair.split("="); + String key1 = keyValue[0]; + String value = keyValue[1]; + map.put(key1, value); } - - if (thisCpuIdIn){ - if (expired){ - // 判断模型使用权限 - // 根据给定的类名初始化类 加密不需要反实例化new - Class buildHeatingScene = Class.forName("com.dky"+"."+ ConfigReader.getProperty(jsonObject.getStr("type"))); - // 实例化这个类 - DntdModelI buildHeatingSceneModel = (DntdModelI)buildHeatingScene.newInstance(); - // 调用指定方法 - jsonObjectResult = buildHeatingSceneModel.createReport(jsonObject,this.specList); - }else { - jsonObjectResult = ResponseUtil.createResponse(Code.KEY_EXPIRATION.getCode(), Code.KEY_EXPIRATION.getDesc(), null); + //获取table表的JSONObject + Map table = mapList.get(0); + //获取statistics表的JSONObject + Map statistics = mapList.get(1); + if(statistics == null) { + System.err.println("未获取到验证信息"); + return ResponseUtil.createResponse(Code.KEY_UNAUTHORIZED.getCode(), Code.KEY_UNAUTHORIZED.getDesc(), null); + } + if(map.get("expireTime") == null){ + return ResponseUtil.createResponse(Code.KEY_CONTEXT_ERROR.getCode(), Code.KEY_CONTEXT_ERROR.getDesc(), null); + } + if(map.get("companyname") == null){ + return ResponseUtil.createResponse(Code.KEY_CONTEXT_ERROR.getCode(), Code.KEY_CONTEXT_ERROR.getDesc(), null); + } + // 定义日期时间格式并转化日期数据 + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + //创建表日期 + Date createTimeDate = null; + try{ + createTimeDate = formatter.parse((String) table.get("CREATE_TIME")); + } catch (NullPointerException e){ + try{ + createTimeDate = formatter.parse((String) statistics.get("create_time")); + } catch (NullPointerException e1){ + e1.printStackTrace(); + return ResponseUtil.createResponse(Code.KEY_UNAUTHORIZED.getCode(), Code.KEY_UNAUTHORIZED.getDesc(), null); } - }else { - jsonObjectResult = ResponseUtil.createResponse(Code.KEY_UNAUTHORIZED.getCode(), Code.KEY_UNAUTHORIZED.getDesc(), null); } - } catch (ClassNotFoundException | NoSuchMethodException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); + //key中验证日期 + Date startTimeDate = formatter.parse(map.get("cpuIds")); + //表创建时间早于key中时间,直接放行 + if (createTimeDate.before(startTimeDate)){ + isAuthorization = Boolean.TRUE; + } + //表创建时间早于key10天内的时间,直接放行 + if (createTimeDate.before(new Date(startTimeDate.getTime() + 1000 * 60 * 60 * 24 * 10))){ + isAuthorization = Boolean.TRUE; + } + if (isAuthorization) { + // 判断模型使用权限 + // 根据给定的类名初始化类 加密不需要反实例化new + Class buildHeatingScene = Class.forName("com.dky" + "." + ConfigReader.getProperty(jsonObject.getStr("type"))); + // 实例化这个类 + DntdModelI buildHeatingSceneModel = (DntdModelI) buildHeatingScene.newInstance(); + // 调用指定方法 + jsonObjectResult = buildHeatingSceneModel.createReport(jsonObject, this.specList); + } else { + jsonObjectResult = ResponseUtil.createResponse(Code.KEY_UNAUTHORIZED.getCode(), Code.KEY_UNAUTHORIZED.getDesc(), null); + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | + NoSuchMethodException | SecurityException e) { + // 异常处理 + System.err.println("反射调用过程中发生异常: " + e.getMessage()); } catch (InvocationTargetException e) { jsonObjectResult = ResponseUtil.createResponse(Code.KEY_CONTEXT_ERROR.getCode(), Code.KEY_CONTEXT_ERROR.getDesc(), null); e.printStackTrace(); diff --git a/file.txt b/file.txt deleted file mode 100644 index 0e39b16..0000000 --- a/file.txt +++ /dev/null @@ -1,3 +0,0 @@ -mxhXSDiPYFjYgzRb -sm4demo123456789 -myhXSDiPUFjYgzRa \ No newline at end of file diff --git a/pom.xml b/pom.xml index 26f40fe..afe8f32 100644 --- a/pom.xml +++ b/pom.xml @@ -27,30 +27,35 @@ cn.hutool hutool-all - 5.4.5 + 5.8.25 + + + org.bouncycastle + bcprov-jdk15to18 + 1.69 - - - - - - - - - - - - - - - - - + + com.dwp + obfuscation + 1.0.0 + + + compile + + obfuscation + + + + + ${basedir} + ${basedir}/target/classes + +