Browse Source

修改加密解密类,保持跨平台结果一致

郑杰 2 years ago
parent
commit
ebab63fac3

+ 0 - 4
admin/pom.xml

@@ -43,10 +43,6 @@
43 43
             <plugin>
44 44
                 <groupId>org.springframework.boot</groupId>
45 45
                 <artifactId>spring-boot-maven-plugin</artifactId>
46
-                <configuration>
47
-                    <!-- maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar -->
48
-                    <includeSystemScope>true</includeSystemScope>
49
-                </configuration>
50 46
             </plugin>
51 47
         </plugins>
52 48
     </build>

+ 0 - 4
buyer-api/pom.xml

@@ -30,10 +30,6 @@
30 30
             <plugin>
31 31
                 <groupId>org.springframework.boot</groupId>
32 32
                 <artifactId>spring-boot-maven-plugin</artifactId>
33
-                <configuration>
34
-                    <!-- maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar -->
35
-                    <includeSystemScope>true</includeSystemScope>
36
-                </configuration>
37 33
             </plugin>
38 34
         </plugins>
39 35
     </build>

+ 0 - 4
common-api/pom.xml

@@ -26,10 +26,6 @@
26 26
             <plugin>
27 27
                 <groupId>org.springframework.boot</groupId>
28 28
                 <artifactId>spring-boot-maven-plugin</artifactId>
29
-                <configuration>
30
-                    <!-- maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar -->
31
-                    <includeSystemScope>true</includeSystemScope>
32
-                </configuration>
33 29
             </plugin>
34 30
         </plugins>
35 31
     </build>

+ 0 - 4
consumer/pom.xml

@@ -37,10 +37,6 @@
37 37
             <plugin>
38 38
                 <groupId>org.springframework.boot</groupId>
39 39
                 <artifactId>spring-boot-maven-plugin</artifactId>
40
-                <configuration>
41
-                    <!-- maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar -->
42
-                    <includeSystemScope>true</includeSystemScope>
43
-                </configuration>
44 40
             </plugin>
45 41
         </plugins>
46 42
     </build>

+ 0 - 10
framework/pom.xml

@@ -413,16 +413,6 @@
413 413
             </exclusions>
414 414
         </dependency>
415 415
 
416
-        <!-- 引入本地lib包 -->
417
-        <!-- 江阴综服AES加密依赖 -->
418
-        <dependency>
419
-            <groupId>com.jyzf</groupId>
420
-            <artifactId>AESEncrypt</artifactId>
421
-            <scope>system</scope>
422
-            <version>1.0.0</version>
423
-            <systemPath>${project.basedir}/src/lib/AESEncrypt.jar</systemPath>
424
-        </dependency>
425
-
426 416
     </dependencies>
427 417
 
428 418
 </project>

+ 133 - 0
framework/src/main/java/cn/lili/common/utils/AesEncrypt.java

@@ -0,0 +1,133 @@
1
+package cn.lili.common.utils;
2
+
3
+import javax.crypto.*;
4
+import javax.crypto.spec.SecretKeySpec;
5
+import java.io.UnsupportedEncodingException;
6
+import java.nio.charset.StandardCharsets;
7
+import java.security.InvalidKeyException;
8
+import java.security.NoSuchAlgorithmException;
9
+import java.security.SecureRandom;
10
+
11
+public class AesEncrypt {
12
+    public AesEncrypt() {
13
+    }
14
+
15
+    public static String encrypt(String content, String password) {
16
+        try {
17
+            KeyGenerator kgen = KeyGenerator.getInstance("AES");
18
+            // 防止Linux下随机生成Key
19
+            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
20
+            secureRandom.setSeed(password.getBytes());
21
+            kgen.init(128, secureRandom);
22
+            // kgen.init(128, new SecureRandom(password.getBytes()));
23
+            SecretKey secretKey = kgen.generateKey();
24
+            byte[] enCodeFormat = secretKey.getEncoded();
25
+            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
26
+            Cipher cipher = Cipher.getInstance("AES");
27
+            byte[] byteContent = content.getBytes("utf-8");
28
+            cipher.init(1, key);
29
+            byte[] result = cipher.doFinal(byteContent);
30
+            return parseByte2HexStr(result);
31
+        } catch (NoSuchAlgorithmException var9) {
32
+            var9.printStackTrace();
33
+        } catch (NoSuchPaddingException var10) {
34
+            var10.printStackTrace();
35
+        } catch (InvalidKeyException var11) {
36
+            var11.printStackTrace();
37
+        } catch (UnsupportedEncodingException var12) {
38
+            var12.printStackTrace();
39
+        } catch (IllegalBlockSizeException var13) {
40
+            var13.printStackTrace();
41
+        } catch (BadPaddingException var14) {
42
+            var14.printStackTrace();
43
+        }
44
+
45
+        return null;
46
+    }
47
+
48
+    public static String decrypt(String encrypt, String password) {
49
+        try {
50
+            byte[] decode = parseHexStr2Byte(encrypt);
51
+            KeyGenerator kgen = KeyGenerator.getInstance("AES");
52
+            // 防止Linux下随机生成Key
53
+            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
54
+            secureRandom.setSeed(password.getBytes());
55
+            kgen.init(128, secureRandom);
56
+            SecretKey secretKey = kgen.generateKey();
57
+            byte[] enCodeFormat = secretKey.getEncoded();
58
+            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
59
+            Cipher cipher = Cipher.getInstance("AES");
60
+            cipher.init(2, key);
61
+            byte[] result = cipher.doFinal(decode);
62
+            return new String(result, StandardCharsets.UTF_8);
63
+        } catch (NoSuchAlgorithmException var9) {
64
+            var9.printStackTrace();
65
+        } catch (NoSuchPaddingException var10) {
66
+            var10.printStackTrace();
67
+        } catch (InvalidKeyException var11) {
68
+            var11.printStackTrace();
69
+        } catch (IllegalBlockSizeException var12) {
70
+            var12.printStackTrace();
71
+        } catch (BadPaddingException var13) {
72
+            var13.printStackTrace();
73
+        }
74
+
75
+        return null;
76
+    }
77
+
78
+    private static String parseByte2HexStr(byte[] buf) {
79
+        StringBuffer sb = new StringBuffer();
80
+
81
+        for(int i = 0; i < buf.length; ++i) {
82
+            String hex = Integer.toHexString(buf[i] & 255);
83
+            if (hex.length() == 1) {
84
+                hex = '0' + hex;
85
+            }
86
+
87
+            sb.append(hex.toUpperCase());
88
+        }
89
+
90
+        return sb.toString();
91
+    }
92
+
93
+    private static byte[] parseHexStr2Byte(String hexStr) {
94
+        if (hexStr.length() < 1) {
95
+            return null;
96
+        } else {
97
+            byte[] result = new byte[hexStr.length() / 2];
98
+
99
+            for(int i = 0; i < hexStr.length() / 2; ++i) {
100
+                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
101
+                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
102
+                result[i] = (byte)(high * 16 + low);
103
+            }
104
+
105
+            return result;
106
+        }
107
+    }
108
+
109
+    public static byte[] encrypt2(String content, String password) {
110
+        try {
111
+            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
112
+            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
113
+            byte[] byteContent = content.getBytes("utf-8");
114
+            cipher.init(1, key);
115
+            byte[] result = cipher.doFinal(byteContent);
116
+            return result;
117
+        } catch (NoSuchAlgorithmException var6) {
118
+            var6.printStackTrace();
119
+        } catch (NoSuchPaddingException var7) {
120
+            var7.printStackTrace();
121
+        } catch (InvalidKeyException var8) {
122
+            var8.printStackTrace();
123
+        } catch (UnsupportedEncodingException var9) {
124
+            var9.printStackTrace();
125
+        } catch (IllegalBlockSizeException var10) {
126
+            var10.printStackTrace();
127
+        } catch (BadPaddingException var11) {
128
+            var11.printStackTrace();
129
+        }
130
+
131
+        return null;
132
+    }
133
+}

+ 2 - 2
framework/src/main/java/cn/lili/modules/order/customs/JiangyinServiceManager.java

@@ -7,6 +7,7 @@ import cn.hutool.core.lang.Assert;
7 7
 import cn.hutool.http.HttpUtil;
8 8
 import cn.hutool.json.JSONUtil;
9 9
 import cn.lili.common.exception.ServiceException;
10
+import cn.lili.common.utils.AesEncrypt;
10 11
 import cn.lili.modules.order.customs.converter.CustomsReqConverter;
11 12
 import cn.lili.modules.order.order.entity.dos.Order;
12 13
 import cn.lili.modules.order.order.entity.dos.OrderCustoms;
@@ -14,7 +15,6 @@ import cn.lili.modules.order.order.entity.enums.PayStatusEnum;
14 15
 import cn.lili.modules.order.order.entity.vo.OrderDetailVO;
15 16
 import cn.lili.modules.order.order.mapper.OrderCustomsMapper;
16 17
 import cn.lili.modules.order.order.mapper.OrderMapper;
17
-import com.dcits.app.encrypt.AESEncrypt;
18 18
 import lombok.extern.slf4j.Slf4j;
19 19
 import org.springframework.beans.factory.annotation.Autowired;
20 20
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -60,7 +60,7 @@ public class JiangyinServiceManager {
60 60
         final String reqMsg = converter.toReq(exchange, payload);
61 61
         System.out.println(reqMsg);
62 62
         customsRec.setReqMsg(reqMsg);
63
-        String encryptContent = AESEncrypt.encrypt(reqMsg, payload.signKey);
63
+        String encryptContent = AesEncrypt.encrypt(reqMsg, payload.signKey);
64 64
         // 构建参数
65 65
         JiangyinServiceParam param = JiangyinServiceParam.builder()
66 66
                 .signMsg(converter.getSign(encryptContent, payload.signKey))

+ 0 - 4
manager-api/pom.xml

@@ -25,10 +25,6 @@
25 25
             <plugin>
26 26
                 <groupId>org.springframework.boot</groupId>
27 27
                 <artifactId>spring-boot-maven-plugin</artifactId>
28
-                <configuration>
29
-                    <!-- maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar -->
30
-                    <includeSystemScope>true</includeSystemScope>
31
-                </configuration>
32 28
             </plugin>
33 29
         </plugins>
34 30
     </build>

+ 0 - 4
seller-api/pom.xml

@@ -25,10 +25,6 @@
25 25
             <plugin>
26 26
                 <groupId>org.springframework.boot</groupId>
27 27
                 <artifactId>spring-boot-maven-plugin</artifactId>
28
-                <configuration>
29
-                    <!-- maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar -->
30
-                    <includeSystemScope>true</includeSystemScope>
31
-                </configuration>
32 28
             </plugin>
33 29
         </plugins>
34 30
     </build>