Browse Source

fix: 内存溢出

liaoyitao 3 months ago
parent
commit
dc775db166
1 changed files with 10 additions and 7 deletions
  1. 10 7
      src/main/java/com/lqkj/link/util/RSAUtils.java

+ 10 - 7
src/main/java/com/lqkj/link/util/RSAUtils.java

@@ -21,13 +21,10 @@ public class RSAUtils {
21
 
21
 
22
     private static Cipher cipher = null;
22
     private static Cipher cipher = null;
23
 
23
 
24
-    public static Cipher getInstance() throws NoSuchPaddingException, NoSuchAlgorithmException {
24
+    public static synchronized void getInstance(Provider provider) throws NoSuchPaddingException, NoSuchAlgorithmException {
25
         if (cipher == null) {
25
         if (cipher == null) {
26
-                Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
27
-                Security.addProvider(provider);
28
                 cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
26
                 cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
29
         }
27
         }
30
-        return cipher;
31
     }
28
     }
32
 
29
 
33
 
30
 
@@ -84,7 +81,9 @@ public class RSAUtils {
84
 
81
 
85
     private static byte[] encrypt(byte[] byteArray){
82
     private static byte[] encrypt(byte[] byteArray){
86
         try {
83
         try {
87
-            Cipher cipher = getInstance();
84
+            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
85
+            Security.addProvider(provider);
86
+            getInstance(provider);
88
             PublicKey publicKey = keyPair.getPublic();
87
             PublicKey publicKey = keyPair.getPublic();
89
             cipher.init(Cipher.ENCRYPT_MODE, publicKey);
88
             cipher.init(Cipher.ENCRYPT_MODE, publicKey);
90
             return cipher.doFinal(byteArray);
89
             return cipher.doFinal(byteArray);
@@ -97,7 +96,9 @@ public class RSAUtils {
97
         try {
96
         try {
98
             //Cipher: 提供加密和解密功能的实例
97
             //Cipher: 提供加密和解密功能的实例
99
             //transformation: "algorithm/mode/padding"
98
             //transformation: "algorithm/mode/padding"
100
-            Cipher cipher = getInstance();
99
+            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
100
+            Security.addProvider(provider);
101
+            getInstance(provider);
101
             PrivateKey privateKey = keyPair.getPrivate();
102
             PrivateKey privateKey = keyPair.getPrivate();
102
             //初始化
103
             //初始化
103
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
104
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
@@ -110,7 +111,9 @@ public class RSAUtils {
110
 
111
 
111
     private static byte[] encrypt(byte[] byteArray, String publicKey){
112
     private static byte[] encrypt(byte[] byteArray, String publicKey){
112
         try {
113
         try {
113
-            Cipher cipher = getInstance();
114
+            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
115
+            Security.addProvider(provider);
116
+            getInstance(provider);
114
             cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
117
             cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
115
             return cipher.doFinal(byteArray);
118
             return cipher.doFinal(byteArray);
116
         } catch (Exception e) {
119
         } catch (Exception e) {