zero il y a 1 an
Parent
commit
d47474f09d

+ 16 - 4
src/main/java/cn/cslg/pas/service/test/PermissionService2.java

@@ -7,20 +7,28 @@ import okhttp3.MediaType;
 import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import okhttp3.RequestBody;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
 import java.util.Objects;
+import java.util.concurrent.TimeUnit;
 
 @Service
 public class PermissionService2 {
+    @Value("${authorUrl}")
+    private String PCSUrl;
 
     public String LogFromPCS(EncryptionLoginDTO encryptionLoginDTO) throws IOException {
         String param = new Gson().toJson(encryptionLoginDTO);
         RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
-        OkHttpClient okHttpClient = new OkHttpClient();
+        OkHttpClient okHttpClient = new OkHttpClient.Builder()
+                .connectTimeout(60, TimeUnit.SECONDS)
+                .writeTimeout(60, TimeUnit.SECONDS)
+                .readTimeout(60, TimeUnit.SECONDS)
+                .build();
         Request request = new Request.Builder()
-                .url("http://47.116.194.135:8085" + "/api/permission/api/admin/loginByEncryption")
+                .url(PCSUrl + "/permission/api/admin/loginByEncryption")
                 .post(requestBody)
                 .build();
         return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
@@ -38,9 +46,13 @@ public class PermissionService2 {
         String param = new Gson().toJson(encryptionFunctionDTO);
 
         RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
-        OkHttpClient okHttpClient = new OkHttpClient();
+        OkHttpClient okHttpClient = new OkHttpClient.Builder()
+                .connectTimeout(60, TimeUnit.SECONDS)
+                .writeTimeout(60, TimeUnit.SECONDS)
+                .readTimeout(60, TimeUnit.SECONDS)
+                .build();
         Request request = new Request.Builder()
-                .url("http://47.116.194.135:8085" + "/api/permission/api/admin/functionByEncryption")
+                .url(PCSUrl + "/permission/api/admin/functionByEncryption")
                 .addHeader("Cookie", "token="+token)
                 .post(requestBody)
                 .build();

+ 45 - 5
src/main/java/cn/cslg/pas/service/test/RSAUtils.java

@@ -3,6 +3,7 @@ package cn.cslg.pas.service.test;
 import org.springframework.stereotype.Component;
 
 import javax.crypto.Cipher;
+import java.io.ByteArrayOutputStream;
 import java.security.*;
 import java.security.spec.PKCS8EncodedKeySpec;
 import java.security.spec.X509EncodedKeySpec;
@@ -22,6 +23,10 @@ public class RSAUtils {
 
     //RSA密钥长度,默认密钥长度是1024,密钥长度必须是64的倍数,在512到65536位之间,不管是RSA还是RSA2长度推荐使用2048
     private static final int KEY_SIZE = 2048;
+    //RSA最大加密明文大小
+    private static final int MAX_ENCRYPT_BLOCK = 245;
+    //RSA最大解密密文大小
+    private static final int MAX_DECRYPT_BLOCK = 256;
 
     /**
      * 生成密钥对
@@ -76,9 +81,26 @@ public class RSAUtils {
         //用公钥初始化此Cipher对象(加密模式)
         cipher.init(Cipher.ENCRYPT_MODE, publicKey);
         //对数据加密
-        byte[] encrypt = cipher.doFinal(data.getBytes());
+        int inputLen = data.getBytes().length;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        int offset = 0;
+        byte[] cache;
+        int i = 0;
+        // 对数据分段加密
+        while (inputLen - offset > 0) {
+            if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
+                cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
+            } else {
+                cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
+            }
+            out.write(cache, 0, cache.length);
+            i++;
+            offset = i * MAX_ENCRYPT_BLOCK;
+        }
+        byte[] encryptedData = out.toByteArray();
+        out.close();
         //返回base64编码后的字符串
-        return Base64.getEncoder().encodeToString(encrypt);
+        return Base64.getEncoder().encodeToString(encryptedData);
     }
 
     /**
@@ -102,10 +124,28 @@ public class RSAUtils {
         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
         //用私钥初始化此Cipher对象(解密模式)
         cipher.init(Cipher.DECRYPT_MODE, privateKey);
-        //对数据解密
-        byte[] decrypt = cipher.doFinal(Base64.getDecoder().decode(data));
+        byte[] decodeBytes = Base64.getDecoder().decode(data);
+        //对数据分段解密
+        int inputLen = decodeBytes.length;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        int offset = 0;
+        byte[] cache;
+        int i = 0;
+        // 对数据分段解密
+        while (inputLen - offset > 0) {
+            if (inputLen - offset > MAX_DECRYPT_BLOCK) {
+                cache = cipher.doFinal(decodeBytes, offset, MAX_DECRYPT_BLOCK);
+            } else {
+                cache = cipher.doFinal(decodeBytes, offset, inputLen - offset);
+            }
+            out.write(cache, 0, cache.length);
+            i++;
+            offset = i * MAX_DECRYPT_BLOCK;
+        }
+        byte[] decryptedData = out.toByteArray();
+        out.close();
         //返回字符串
-        return new String(decrypt);
+        return new String(decryptedData, "UTF-8");
     }
 
     /**

Fichier diff supprimé car celui-ci est trop grand
+ 25 - 7
src/test/java/cn/cslg/pas/test/TempServiceTests.java