ソースを参照

fix: 内存溢出

liaoyitao 3 ヶ月 前
コミット
dc775db166
共有1 個のファイルを変更した10 個の追加7 個の削除を含む
  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 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 25
         if (cipher == null) {
26
-                Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
27
-                Security.addProvider(provider);
28 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 82
     private static byte[] encrypt(byte[] byteArray){
86 83
         try {
87
-            Cipher cipher = getInstance();
84
+            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
85
+            Security.addProvider(provider);
86
+            getInstance(provider);
88 87
             PublicKey publicKey = keyPair.getPublic();
89 88
             cipher.init(Cipher.ENCRYPT_MODE, publicKey);
90 89
             return cipher.doFinal(byteArray);
@@ -97,7 +96,9 @@ public class RSAUtils {
97 96
         try {
98 97
             //Cipher: 提供加密和解密功能的实例
99 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 102
             PrivateKey privateKey = keyPair.getPrivate();
102 103
             //初始化
103 104
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
@@ -110,7 +111,9 @@ public class RSAUtils {
110 111
 
111 112
     private static byte[] encrypt(byte[] byteArray, String publicKey){
112 113
         try {
113
-            Cipher cipher = getInstance();
114
+            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
115
+            Security.addProvider(provider);
116
+            getInstance(provider);
114 117
             cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
115 118
             return cipher.doFinal(byteArray);
116 119
         } catch (Exception e) {