Browse Source

fix: 内存溢出

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