Browse Source

fix: 内存溢出

liaoyitao 3 months ago
parent
commit
49f1a4cce4
1 changed files with 16 additions and 9 deletions
  1. 16 9
      src/main/java/com/lqkj/link/util/RSAUtils.java

+ 16 - 9
src/main/java/com/lqkj/link/util/RSAUtils.java

@@ -1,6 +1,7 @@
1
 package com.lqkj.link.util;
1
 package com.lqkj.link.util;
2
 
2
 
3
 import javax.crypto.Cipher;
3
 import javax.crypto.Cipher;
4
+import javax.crypto.NoSuchPaddingException;
4
 import java.nio.charset.StandardCharsets;
5
 import java.nio.charset.StandardCharsets;
5
 import java.security.*;
6
 import java.security.*;
6
 import java.security.spec.InvalidKeySpecException;
7
 import java.security.spec.InvalidKeySpecException;
@@ -18,6 +19,18 @@ public class RSAUtils {
18
     //KeyPair is a simple holder for a key pair.
19
     //KeyPair is a simple holder for a key pair.
19
     private static final KeyPair keyPair = initKey();
20
     private static final KeyPair keyPair = initKey();
20
 
21
 
22
+    private static Cipher cipher = null;
23
+
24
+    public static Cipher getInstance() throws NoSuchPaddingException, NoSuchAlgorithmException {
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);
29
+        }
30
+        return cipher;
31
+    }
32
+
33
+
21
     /**
34
     /**
22
      * 初始化方法,产生key pair,提供provider和random
35
      * 初始化方法,产生key pair,提供provider和random
23
      *
36
      *
@@ -71,9 +84,7 @@ public class RSAUtils {
71
 
84
 
72
     private static byte[] encrypt(byte[] byteArray){
85
     private static byte[] encrypt(byte[] byteArray){
73
         try {
86
         try {
74
-            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
75
-            Security.addProvider(provider);
76
-            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
87
+            Cipher cipher = getInstance();
77
             PublicKey publicKey = keyPair.getPublic();
88
             PublicKey publicKey = keyPair.getPublic();
78
             cipher.init(Cipher.ENCRYPT_MODE, publicKey);
89
             cipher.init(Cipher.ENCRYPT_MODE, publicKey);
79
             return cipher.doFinal(byteArray);
90
             return cipher.doFinal(byteArray);
@@ -84,11 +95,9 @@ public class RSAUtils {
84
 
95
 
85
     private static byte[] decrypt(byte[] byteArray) {
96
     private static byte[] decrypt(byte[] byteArray) {
86
         try {
97
         try {
87
-            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
88
-            Security.addProvider(provider);
89
             //Cipher: 提供加密和解密功能的实例
98
             //Cipher: 提供加密和解密功能的实例
90
             //transformation: "algorithm/mode/padding"
99
             //transformation: "algorithm/mode/padding"
91
-            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
100
+            Cipher cipher = getInstance();
92
             PrivateKey privateKey = keyPair.getPrivate();
101
             PrivateKey privateKey = keyPair.getPrivate();
93
             //初始化
102
             //初始化
94
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
103
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
@@ -101,9 +110,7 @@ public class RSAUtils {
101
 
110
 
102
     private static byte[] encrypt(byte[] byteArray, String publicKey){
111
     private static byte[] encrypt(byte[] byteArray, String publicKey){
103
         try {
112
         try {
104
-            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
105
-            Security.addProvider(provider);
106
-            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
113
+            Cipher cipher = getInstance();
107
             cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
114
             cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
108
             return cipher.doFinal(byteArray);
115
             return cipher.doFinal(byteArray);
109
         } catch (Exception e) {
116
         } catch (Exception e) {