Browse Source

fix: 内存溢出

liaoyitao 5 months ago
parent
commit
5ba0ff7142
1 changed files with 7 additions and 16 deletions
  1. 7 16
      src/main/java/com/lqkj/link/util/RSAUtils.java

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

@@ -1,7 +1,8 @@
1
 package com.lqkj.link.util;
1
 package com.lqkj.link.util;
2
 
2
 
3
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
4
+
3
 import javax.crypto.Cipher;
5
 import javax.crypto.Cipher;
4
-import javax.crypto.NoSuchPaddingException;
5
 import java.nio.charset.StandardCharsets;
6
 import java.nio.charset.StandardCharsets;
6
 import java.security.*;
7
 import java.security.*;
7
 import java.security.spec.InvalidKeySpecException;
8
 import java.security.spec.InvalidKeySpecException;
@@ -17,15 +18,9 @@ import java.util.Base64;
17
  **/
18
  **/
18
 public class RSAUtils {
19
 public class RSAUtils {
19
     //KeyPair is a simple holder for a key pair.
20
     //KeyPair is a simple holder for a key pair.
21
+    private static BouncyCastleProvider provider = new BouncyCastleProvider();
20
     private static final KeyPair keyPair = initKey();
22
     private static final KeyPair keyPair = initKey();
21
 
23
 
22
-    private static Cipher cipher = null;
23
-
24
-    public static synchronized void getInstance(Provider provider) throws NoSuchPaddingException, NoSuchAlgorithmException {
25
-        if (cipher == null) {
26
-                cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
27
-        }
28
-    }
29
 
24
 
30
 
25
 
31
     /**
26
     /**
@@ -36,7 +31,6 @@ public class RSAUtils {
36
     private static KeyPair initKey() {
31
     private static KeyPair initKey() {
37
         try {
32
         try {
38
             //添加provider
33
             //添加provider
39
-            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
40
             Security.addProvider(provider);
34
             Security.addProvider(provider);
41
             //产生用于安全加密的随机数
35
             //产生用于安全加密的随机数
42
             SecureRandom random = new SecureRandom();
36
             SecureRandom random = new SecureRandom();
@@ -81,9 +75,8 @@ public class RSAUtils {
81
 
75
 
82
     private static byte[] encrypt(byte[] byteArray){
76
     private static byte[] encrypt(byte[] byteArray){
83
         try {
77
         try {
84
-            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
85
             Security.addProvider(provider);
78
             Security.addProvider(provider);
86
-            getInstance(provider);
79
+            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
87
             PublicKey publicKey = keyPair.getPublic();
80
             PublicKey publicKey = keyPair.getPublic();
88
             cipher.init(Cipher.ENCRYPT_MODE, publicKey);
81
             cipher.init(Cipher.ENCRYPT_MODE, publicKey);
89
             return cipher.doFinal(byteArray);
82
             return cipher.doFinal(byteArray);
@@ -94,11 +87,10 @@ public class RSAUtils {
94
 
87
 
95
     private static byte[] decrypt(byte[] byteArray) {
88
     private static byte[] decrypt(byte[] byteArray) {
96
         try {
89
         try {
90
+            Security.addProvider(provider);
97
             //Cipher: 提供加密和解密功能的实例
91
             //Cipher: 提供加密和解密功能的实例
98
             //transformation: "algorithm/mode/padding"
92
             //transformation: "algorithm/mode/padding"
99
-            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
100
-            Security.addProvider(provider);
101
-            getInstance(provider);
93
+            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
102
             PrivateKey privateKey = keyPair.getPrivate();
94
             PrivateKey privateKey = keyPair.getPrivate();
103
             //初始化
95
             //初始化
104
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
96
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
@@ -111,9 +103,8 @@ public class RSAUtils {
111
 
103
 
112
     private static byte[] encrypt(byte[] byteArray, String publicKey){
104
     private static byte[] encrypt(byte[] byteArray, String publicKey){
113
         try {
105
         try {
114
-            Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
115
             Security.addProvider(provider);
106
             Security.addProvider(provider);
116
-            getInstance(provider);
107
+            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
117
             cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
108
             cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
118
             return cipher.doFinal(byteArray);
109
             return cipher.doFinal(byteArray);
119
         } catch (Exception e) {
110
         } catch (Exception e) {