Sfoglia il codice sorgente

增加注册码模块工具类

xiewj 2 anni fa
parent
commit
a24acd8ee1

+ 41 - 0
4dkankan-utils-reg/pom.xml

@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>4dkankan-utils</artifactId>
+        <groupId>com.fdkankan</groupId>
+        <version>3.0.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>4dkankan-utils-reg</artifactId>
+
+    <properties>
+        <fastjson.version>2.0.6</fastjson.version>
+        <hutool.version>5.8.6</hutool.version>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>1.2.11</version>
+        </dependency>
+        <!-- 阿里JSON解析器 -->
+        <dependency>
+            <groupId>com.alibaba.fastjson2</groupId>
+            <artifactId>fastjson2</artifactId>
+            <version>${fastjson.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>${hutool.version}</version>
+        </dependency>
+
+
+    </dependencies>
+
+</project>

+ 39 - 0
4dkankan-utils-reg/src/main/java/com/fdkankan/reg/CompressionUtils.java

@@ -0,0 +1,39 @@
+package com.fdkankan.reg;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+/**
+ * CompressionUtils 压缩字符串
+ */
+public class CompressionUtils {
+
+    public static String compressAndEncode(String data) throws IOException {
+        byte[] input = data.getBytes("UTF-8");
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
+        gzipOutputStream.write(input);
+        gzipOutputStream.close();
+        byte[] compressed = outputStream.toByteArray();
+        byte[] encoded = Base64.getEncoder().encode(compressed);
+        return new String(encoded, "UTF-8");
+    }
+
+    public static String decodeAndDecompress(String encodedData) throws IOException {
+        byte[] encoded = encodedData.getBytes("UTF-8");
+        byte[] compressed = Base64.getDecoder().decode(encoded);
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(compressed);
+        GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        byte[] buffer = new byte[1024];
+        int length;
+        while ((length = gzipInputStream.read(buffer)) > 0) {
+            outputStream.write(buffer, 0, length);
+        }
+        gzipInputStream.close();
+        return new String(outputStream.toByteArray(), "UTF-8");
+    }
+}

+ 114 - 0
4dkankan-utils-reg/src/main/java/com/fdkankan/reg/Crypt.java

@@ -0,0 +1,114 @@
+package com.fdkankan.reg;
+
+import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.util.Base64;
+/***
+* @description:激光相机本地版加密解密SNcode配合laserGen项目使用
+* @author: Xiewj
+* @date: 2023/6/14 18:19
+* @param:
+* @return:
+**/
+public class Crypt {
+   private static String key = "4dage1684dage168";
+
+
+   //用于构建十六进制输出
+   private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
+   /**
+    * AES解密 128 AES/ECB/PKCS5Padding
+    *
+    * @param source      需要解密的字符串
+    * @param hexOrBase64 true:hex,false:base64
+    * @return 返回解密结果
+    */
+   public static String decryptAES(String source, boolean hexOrBase64) {
+      try {
+         SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");
+         Cipher cipher = Cipher.getInstance("AES");
+         cipher.init(Cipher.DECRYPT_MODE, secretKey);
+         if (hexOrBase64) {
+            return new String(cipher.doFinal (hex2bytes(source))).toUpperCase();
+         } else {
+            return new String(cipher.doFinal( decodeBase64(source))).toUpperCase();
+         }
+      } catch (Exception e) {
+         System.out.println(e.getMessage());
+         throw new RuntimeException("出错啦!", e);
+      }
+   }
+   /**
+    * AES加密 128 AES/ECB/PKCS5Padding
+    *
+    * @param source      需要加密的字符串
+    * @param hexOrBase64 true:hex,false:base64
+    * @return 返回加密结果
+    */
+   public static String encryptAES(String source,   boolean hexOrBase64) {
+      try {
+         SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");
+         Cipher cipher = Cipher.getInstance("AES");
+         cipher.init(Cipher.ENCRYPT_MODE, secretKey);
+         if (hexOrBase64) {
+            return new String( bytes2hex(cipher.doFinal(source.getBytes()))).toUpperCase();
+         } else {
+            return  encodeBase64(cipher.doFinal(source.getBytes())).toUpperCase();
+         }
+      } catch (Exception e) {
+         throw new RuntimeException("出错啦!", e);
+      }
+
+   }
+
+   public static byte[] hex2bytes(String hexStr) {
+      byte[] bytes = new byte[hexStr.length() / 2];
+      for (int i = 0; i < hexStr.length(); i += 2) {
+         bytes[i / 2] = (byte) ((Character.digit(hexStr.charAt(i), 16) << 4)
+                 + Character.digit(hexStr.charAt(i+1), 16));
+      }
+      return bytes;
+   }
+
+
+   public static byte[] decodeBase64(String text) {
+      return java.util.Base64.getDecoder().decode(text);
+   }
+
+
+   public static char[] bytes2hex(final byte[] data) {
+      final int l = data.length;
+      final char[] out = new char[l << 1];
+      // two characters form the hex value.
+      for (int i = 0, j = 0; i < l; i++) {
+         out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
+         out[j++] = DIGITS_LOWER[0x0F & data[i]];
+      }
+      return out;
+   }
+
+
+   public static String encodeBase64(byte[] data) {
+      return Base64.getEncoder().encodeToString(data);
+   }
+
+
+
+   public static void main(String[] args) throws Exception {
+      String value ="A5C140AB33E4F6F1BA6B675875A424BB22BF15DD6DE1A4A5933F627B89BFADCA033FBF152BB9966B8489EDD346201562";
+      //"HmtaYAN+n8dzKifi50/sA4Sv/dLKR5MnfZKcJVSzmST0D7ZkbmM8AebuNNkKBc4Z7DoXDnDwwwap2hg+6ztWQAZdQZOfAHR5bueqpApDN9il6gIuQadcdPcvrurRRiTifOQYsE9nRQKy6smol0jrz0wLEDxyPh3ZfBZG0e5KxrEq6Es6E3/05F4jIBYy+dUtvYBPSrVi3kzkblFex6qZOf/6jbtSlchWJVXMuEgA05LplUlUvNr5vwNC65KIy6beHcyhntIO2wJHx5uknjtidXUft3s87ZlxTzMEiKV6cXU9ejuCxdOz9n0c7UPeAOqHprxW2FvOhytVSMoSPP6No9pSochmKdXBPgOQd15rswBi0ejSZ+qWG4/MrBhVFD5OaQ8Cr7wy9hoWdnYrd0tcxPZv4EiwF3DDqctffYgQjy8=";
+      //"openid=ouQdHv18KY5HPhW16cDk43Y5-yyk&nickname=%e6%9d%8e%e5%a8%81&headimg=http%3a%2f%2fthirdwx.qlogo.cn%2fmmopen%2fajNVdqHZLLB6qkUjQibrUJniaBhLf8hjS5L4roMR84T62m7NyHiaIAXKh1cl2vJwfdSfEzju8HicugEXbJQFHx1pZw%2f132&rytype=1&nwflag=1&empno=BEJY3293&stime=1603340863000";
+      String key = "4dage1684dage168";
+
+//      //加密
+      String s = encryptAES(value, true);
+      System.out.println(s);
+      //解密
+      String k = decryptAES("9013E47C4A1181E70B697E956F7D8F16E776FB0F0D14FEF35C31158D889EAED9C0AACA53E67FE3F54468DE6F90B123E24E663E0AED062C1258A2CF937B17451958D115C4DDC7C317E1E06C174B748570F8618BBFD7A5AF56CFDDF48E2112BBD5033FBF152BB9966B8489EDD346201562", true);
+      System.out.println(k);
+
+   }
+
+}

+ 117 - 0
4dkankan-utils-reg/src/main/java/com/fdkankan/reg/DeflaterUtils.java

@@ -0,0 +1,117 @@
+package com.fdkankan.reg;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.zip.DataFormatException;
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
+import java.util.zip.InflaterInputStream;
+
+/**
+    * DeflaterUtils 压缩字符串
+    */
+public class DeflaterUtils {
+      /**
+       * 压缩
+       */
+      public static String zipString(String unzipString) {
+
+         //使用指定的压缩级别创建一个新的压缩器。
+         Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
+         //设置压缩输入数据。
+         deflater.setInput(unzipString.getBytes());
+         //当被调用时,表示压缩应该以输入缓冲区的当前内容结束。
+         deflater.finish();
+
+         final byte[] bytes = new byte[1024];
+         ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
+
+         while (!deflater.finished()) {
+            //压缩输入数据并用压缩数据填充指定的缓冲区。
+            int length = deflater.deflate(bytes);
+            outputStream.write(bytes, 0, length);
+         }
+         //关闭压缩器并丢弃任何未处理的输入。
+         deflater.end();
+         System.out.println(outputStream.toByteArray());
+         return Base64.getEncoder().encodeToString(outputStream.toByteArray());
+      }
+
+      /**
+       * 解压缩
+       */
+      public static String unzipString(String zipString) {
+         byte[] decode = Base64.getMimeDecoder().decode(zipString);
+         //创建一个新的解压缩器  https://www.yiibai.com/javazip/javazip_inflater.html
+
+         Inflater inflater = new Inflater();
+         //设置解压缩的输入数据。
+         inflater.setInput(decode);
+         final byte[] bytes = new byte[1024];
+         ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
+         try {
+            //finished() 如果已到达压缩数据流的末尾,则返回true。
+            while (!inflater.finished()) {
+               //将字节解压缩到指定的缓冲区中。
+               int length = inflater.inflate(bytes);
+               outputStream.write(bytes, 0, length);
+            }
+         } catch (DataFormatException e) {
+            e.printStackTrace();
+            return null;
+         } finally {
+            //关闭解压缩器并丢弃任何未处理的输入。
+            inflater.end();
+         }
+
+         return outputStream.toString();
+      }
+      public static byte[] compress(String data) throws IOException {
+         byte[] input = data.getBytes("UTF-8");
+         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+         Deflater compressor = new Deflater();
+         compressor.setLevel(Deflater.BEST_COMPRESSION);
+         compressor.setInput(input);
+         compressor.finish();
+         byte[] buffer = new byte[1024];
+         while (!compressor.finished()) {
+            int count = compressor.deflate(buffer);
+            outputStream.write(buffer, 0, count);
+         }
+         outputStream.close();
+         return outputStream.toByteArray();
+      }
+
+      public static String decompress(byte[] data) throws IOException {
+         Inflater decompressor = new Inflater();
+         decompressor.setInput(data);
+         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+         InflaterInputStream inflaterInputStream = new InflaterInputStream(
+                 new ByteArrayInputStream(data), decompressor);
+         byte[] buffer = new byte[1024];
+         int length;
+         while ((length = inflaterInputStream.read(buffer)) > 0) {
+            outputStream.write(buffer, 0, length);
+         }
+         outputStream.close();
+         byte[] output = outputStream.toByteArray();
+         return new String(output, "UTF-8");
+      }
+   public static void main(String[] args) throws IOException {
+      String data  = "9013E47C4A1181E70B697E956F7D8F16E776FB0F0D14FEF35C31158D889EAED9C0AACA53E67FE3F54468DE6F90B123E24E663E0AED062C1258A2CF937B17451958D115C4DDC7C317E1E06C174B748570F8618BBFD7A5AF56CFDDF48E2112BBD56B714370A75A094E3CADC98B349921755DCDAE2BC2660A29D4E33E34FA6AC5DC9B955DA1E43D35CDCBBB2956440FB83120CFD7E09EB4D8B62F5C981FD8D12798";
+      System.out.println(data.length());
+      String formattedString = DeflaterUtils.zipString(data);
+      System.out.println(formattedString.length());
+      System.out.println(formattedString);
+
+      // 还原为字符串
+      String restoredData = DeflaterUtils.unzipString(formattedString);
+      System.out.println(restoredData.length());
+      System.out.println(restoredData);
+      byte[] compress = compress(data);
+
+
+   }
+}

+ 167 - 0
4dkankan-utils-reg/src/main/java/com/fdkankan/reg/RegCodeUtil.java

@@ -0,0 +1,167 @@
+package com.fdkankan.reg;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import com.fdkankan.reg.dto.CamRegDto;
+import com.fdkankan.reg.dto.CamRegSDto;
+import com.fdkankan.reg.dto.MachineRegDto;
+import lombok.extern.slf4j.Slf4j;
+import java.util.ArrayList;
+import java.util.List;
+import cn.hutool.core.util.ObjUtil;
+
+/**
+ * @author Xiewj
+ * @date 2023/7/6
+ */
+@Slf4j
+public class RegCodeUtil {
+
+    /***
+     * @description:生成安装码
+     * @author: Xiewj
+     * @date: 2023/7/6 15:01
+     * @param:  machineCode 机器码
+     * @return:
+     **/
+    public static String GenRegeditCode(String machineCode){
+        String s = Crypt.encryptAES(machineCode, true);
+        log.info("安装注册码:{}",s);
+        return s;
+    }
+
+    /***
+     * @description:生成相机激活码
+     * @author: Xiewj
+     * @date: 2023/7/6 15:01
+     * @param:  plaintext 需要加密的字符串 [{"SN": "SN000001", "TOSN": "", "OPTION": "ADD"}]
+     * @return:
+     **/
+    public static String GenRegeditCamCode(String plaintext){
+            String s = Crypt.encryptAES(plaintext, true);
+        log.info("相机注册码:{}",s);
+        log.info("相机注册码原始长度:{}",s.length());
+        String code = DeflaterUtils.zipString(s);
+        log.info("相机注册码压缩:{}",code);
+        log.info("相机注册码压缩长度:{}",code.length());
+
+        return code;
+    }
+    /***
+     * @description 获取激活数据
+     * @author: Xiewj
+     * @date: 2023/7/6 15:01
+     * @param:  plaintext 需要转换成对象的字符串
+     * @return:
+     **/
+    public static MachineRegDto ParseMachineCode(String plaintext){
+        try {
+            String source = Crypt.decryptAES(plaintext, true);
+            log.info("相机注册码解密:{}",source);
+            MachineRegDto t = JSON.toJavaObject(JSONObject.parseObject(source) , MachineRegDto.class);
+            return t;
+        }catch (Exception e){
+            e.printStackTrace();
+            return null;
+        }
+    }
+    /***
+     * @description 获取激活数据
+     * @author: Xiewj
+     * @date: 2023/7/6 15:01
+     * @param:  plaintext 需要转换成对象的字符串
+     * @return:
+     **/
+    public static CamRegDto ParseRegeditCamCode(String machineCode , String plaintext){
+        try {
+            //解压
+            String code = DeflaterUtils.unzipString(plaintext);
+            log.info("相机注册码解压:{}",code);
+            String source = Crypt.decryptAES(code, true);
+            log.info("相机注册码解密:{}",source);
+            CamRegDto t = JSON.toJavaObject(JSONObject.parseObject(source) , CamRegDto.class);
+            if (ObjUtil.isNull(t.getMachineCode()) && !t.getMachineCode().equals(machineCode)){
+                throw new RuntimeException("校验失败");
+            }
+            return t;
+        }catch (Exception e){
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    /***
+     * @description:生成相机激活码多个
+     * @author: Xiewj
+     * @date: 2023/7/6 15:01
+     * @param:  plaintext 需要加密的字符串 [{"SN": "SN000001", "TOSN": ""},{"SN": "SN000001", "TOSN": ""}]
+     * @return:
+     **/
+    public static String batchGenRegeditCamCode( CamRegSDto dtos){
+        String camCode = GenRegeditCamCode(JSONObject.toJSONString(dtos).toString());
+        return camCode;
+    }
+    /***
+     * @description 获取激活数据
+     * @author: Xiewj
+     * @date: 2023/7/6 15:01
+     * @param:  plaintext 需要转换成对象的字符串
+     * @return:
+     **/
+    public static CamRegSDto BatchParseRegeditCamCode(String machineCode,String plaintext){
+        try {
+            //解压
+            String code = DeflaterUtils.unzipString(plaintext);
+            log.info("批量生成相机码解压:{}",code);
+            String source = Crypt.decryptAES(code, true);
+            CamRegSDto t = JSON.toJavaObject(JSONObject.parseObject(source) , CamRegSDto.class);
+            if (ObjUtil.isNull(t.getMachineCode()) && !t.getMachineCode().equals(machineCode)){
+                throw new RuntimeException("校验失败");
+            }
+            return t;
+        }catch (Exception e){
+            e.printStackTrace();
+            return null;
+        }
+    }
+    public static void main(String[] args) {
+        //读取机器码
+        MachineRegDto machineRegDto = ParseMachineCode("E4E73A8EB46B8EA252B9C1B34048F60679FB345BC6F233A8369E355828B68F69FDAB2150B286C0E0A397D960183716F8BF4C8B086D4C5BB5D1243CB9AA924B6B8A0ED22ED413BD27681CE5C36198EBF7FE06E75CD444B46A7721E2462DD234EB6B5B780F1FB9CF3C26C3ED4DB7B08B20");
+        System.out.println(machineRegDto);
+
+
+        String machineCode="E4E73A8EB46B8EA252B9C1B34048F60679FB345BC6F233A8369E355828B68F69FDAB2150B286C0E0A397D960183716F8BF4C8B086D4C5BB5D1243CB9AA924B6B8A0ED22ED413BD27681CE5C36198EBF7FE06E75CD444B46A7721E2462DD234EB6B5B780F1FB9CF3C26C3ED4DB7B08B20";
+        //生成安装注册码
+        String s = GenRegeditCode(machineCode);
+
+        CamRegDto camRegDto1=new CamRegDto();
+        camRegDto1.setSn("00000001");
+        camRegDto1.setMachineCode(machineCode);
+        //生成相机注册码
+        String camCode = GenRegeditCamCode( JSON.toJSONString(camRegDto1));
+        //校验相机注册码
+        CamRegDto camRegDto3 = ParseRegeditCamCode(machineCode,camCode);
+
+
+        List<CamRegDto> list=new ArrayList<>();
+
+        CamRegDto camRegDto=new CamRegDto();
+        camRegDto.setSn("00000001");
+        list.add(camRegDto);
+
+        CamRegDto camRegDto2=new CamRegDto();
+        camRegDto2.setSn("00000001");
+        list.add(camRegDto2);
+
+        CamRegSDto CamReg=new CamRegSDto();
+        CamReg.setMachineCode(machineCode);
+        CamReg.setCamRegs(list);
+
+        String camCode1 = batchGenRegeditCamCode( CamReg);
+
+//        List<CamRegDto> list1 = BatchParseRegeditCamCode(machineCode+"a",camCode1);
+        CamRegSDto list1 = BatchParseRegeditCamCode(machineCode,camCode1);
+        log.info("结束");
+    }
+
+}

+ 17 - 0
4dkankan-utils-reg/src/main/java/com/fdkankan/reg/dto/CamRegDto.java

@@ -0,0 +1,17 @@
+package com.fdkankan.reg.dto;
+
+import lombok.Data;
+
+/**
+ * Created by Xiewj on 2021/8/17 0017 8:49
+ */
+@Data
+public class CamRegDto {
+    private String machineCode;
+     /**
+     * {"SN": "SN000001", "TOSN": "", "OPTION": "ADD"}
+     **/
+    private String sn;
+    private String toSn;
+
+}

+ 17 - 0
4dkankan-utils-reg/src/main/java/com/fdkankan/reg/dto/CamRegSDto.java

@@ -0,0 +1,17 @@
+package com.fdkankan.reg.dto;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * Created by Xiewj on 2021/8/17 0017 8:49
+ */
+@Data
+public class CamRegSDto {
+    private String machineCode;
+    private Long timestamp=System.currentTimeMillis();
+
+    List<CamRegDto> camRegs;
+
+}

+ 13 - 0
4dkankan-utils-reg/src/main/java/com/fdkankan/reg/dto/MachineRegDto.java

@@ -0,0 +1,13 @@
+package com.fdkankan.reg.dto;
+
+import lombok.Data;
+
+/**
+ * Created by Xiewj on 2021/8/17 0017 8:49
+ */
+@Data
+public class MachineRegDto {
+    private String uuid;
+    private String computerName;
+
+}

+ 1 - 0
pom.xml

@@ -27,6 +27,7 @@
         <module>4dkankan-utils-image</module>
         <module>4dkankan-utils-sensitive-word</module>
         <module>4dkankan-utils-elasticsearch</module>
+        <module>4dkankan-utils-reg</module>
     </modules>
 
     <groupId>com.fdkankan</groupId>