我正在使用 bouncycastle.jce.provider 运行 java (openjdk:11.0.8) 应用程序:
group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.65'
我注意到内存泄漏,转储显示几乎所有内存都被
javax.crypto.JceSecurity -- > java.util.IdentityHashMap
这是它的样子:
看起来 hashMap 越来越大了。我在 JceSecurity 中看到 2 个 IdentityHashMaps,其中指出:
// Map<Provider,?> of the providers we already have verified
// value == PROVIDER_VERIFIED is successfully verified
// value is failure cause Exception in error case
private static final Map<Provider, Object> verificationResults =
new IdentityHashMap<>();
// Map<Provider,?> of the providers currently being verified
private static final Map<Provider, Object> verifyingProviders =
new IdentityHashMap<>();
如何克服?修复如何?
我在使用此提供程序的方式下方添加,以防它以某种方式相关:
static {
Security.addProvider(new BouncyCastleProvider());
}
public static String encrypt(String pkcs8Base64PublicKey,String text) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
String publicKeyStr = canonizeKey(pkcs8Base64PublicKey);
PublicKey publicKey = toPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance(DEFAULT_TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedText = cipher.doFinal(text.getBytes(Charset.forName("UTF-8")));
return new String(Base64.getEncoder().encode(encryptedText), Charset.forName("UTF-8"));
}
public static String decrypt(String pkcs8Base64PrivateKey, String encryptedMessage) throws GeneralSecurityException {
String privateKeyStr = canonizeKey(pkcs8Base64PrivateKey);
PrivateKey privateKey = toPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance(DEFAULT_TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedMessage = Base64.getDecoder().decode(encryptedMessage);
return new String(cipher.doFinal(decryptedMessage), Charset.forName("UTF-8"));
}