Skip to content

Commit 44864f1

Browse files
committed
重要Bug修复:加解密中,不能再代码里new BouncyCastleProvider(),JceSecurity. getVerificationResult内部会进行判断,如果是新值,则每次都会put到map中,导致内存缓便被耗尽,程序假死崩溃;
1 parent f50d915 commit 44864f1

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

‎src/main/java/cn/huanzi/qch/baseadmin/util/AesUtil.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public class AesUtil {
3535
*/
3636
public static String key;
3737

38+
/**
39+
* 不能在代码中创建
40+
* JceSecurity.getVerificationResult 会将其put进private static final lap<Provider,0bject>中,导致内存缓便被耗尽
41+
*/
42+
private static final BouncyCastleProvider PROVIDER = new BouncyCastleProvider();
43+
3844
static {
3945
key = getKey();
4046
}
@@ -77,14 +83,12 @@ public static String getKey() {
7783
*/
7884
public static String encrypt(String content, String encryptKey) throws Exception {
7985
//设置Cipher对象
80-
Cipher cipher = Cipher.getInstance(ALGORITHMS,new BouncyCastleProvider());
86+
Cipher cipher = Cipher.getInstance(ALGORITHMS, PROVIDER);
8187
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), KEY_ALGORITHM));
8288

8389
//调用doFinal
84-
byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
85-
8690
// 转base64
87-
return Base64.encodeBase64String(b);
91+
return Base64.encodeBase64String(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
8892

8993
}
9094

@@ -99,12 +103,11 @@ public static String decrypt(String encryptStr, String decryptKey) throws Except
99103
byte[] decodeBase64 = Base64.decodeBase64(encryptStr);
100104

101105
//设置Cipher对象
102-
Cipher cipher = Cipher.getInstance(ALGORITHMS,new BouncyCastleProvider());
106+
Cipher cipher = Cipher.getInstance(ALGORITHMS,PROVIDER);
103107
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), KEY_ALGORITHM));
104108

105109
//调用doFinal解密
106-
byte[] decryptBytes = cipher.doFinal(decodeBase64);
107-
return new String(decryptBytes);
110+
return new String(cipher.doFinal(decodeBase64));
108111
}
109112

110113
}

‎src/main/java/cn/huanzi/qch/baseadmin/util/RsaUtil.java‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ private static Map<String, Object> genKeyPair() throws Exception {
9595
*/
9696
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
9797
//base64格式的key字符串转Key对象
98-
byte[] keyBytes = Base64.decodeBase64(privateKey);
99-
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
100-
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
101-
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
98+
Key privateK = KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)));
10299

103100
//设置加密、填充方式
104101
/*
@@ -126,10 +123,7 @@ public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey
126123
*/
127124
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
128125
//base64格式的key字符串转Key对象
129-
byte[] keyBytes = Base64.decodeBase64(publicKey);
130-
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
131-
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
132-
Key publicK = keyFactory.generatePublic(x509KeySpec);
126+
Key publicK = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(publicKey)));
133127

134128
//设置加密、填充方式
135129
/*
@@ -185,8 +179,7 @@ private static byte[] encryptAndDecryptOfSubsection(byte[] data, Cipher cipher,
185179
i++;
186180
offSet = i * encryptBlock;
187181
}
188-
byte[] toByteArray = out.toByteArray();
189182
out.close();
190-
return toByteArray;
183+
return out.toByteArray();
191184
}
192185
}

0 commit comments

Comments
 (0)