Java bouncycastle.jce.provider上的内存泄漏
我正在使用bouncycastle.jce.provider运行一个java(openjdk:11.0.8)应用程序:Java bouncycastle.jce.provider上的内存泄漏,java,bouncycastle,java-11,java-security,java-security-manager,Java,Bouncycastle,Java 11,Java Security,Java Security Manager,我正在使用bouncycastle.jce.provider运行一个java(openjdk:11.0.8)应用程序: group:'org.bouncycastle',名称:'bcprov-jdk15on',版本:'1.65' 我注意到内存泄漏,转储显示几乎所有内存都被 javax.crypto.JceSecurity-->java.util.IdentityHashMap 这就是它的样子: 看起来hashMap越来越大了。我在JceSecurity中看到两个身份验证图,其中说明: // M
group:'org.bouncycastle',名称:'bcprov-jdk15on',版本:'1.65'
我注意到内存泄漏,转储显示几乎所有内存都被
javax.crypto.JceSecurity-->java.util.IdentityHashMap
这就是它的样子:
看起来hashMap越来越大了。我在JceSecurity中看到两个身份验证图,其中说明:
// 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<>();
我也有同样的问题,我可以用这个小代码解决它。我希望这能帮助某人:)
您可能想查看Saw,但我不知道是否有修复、解决方法或某种替换方法。错误报告中提到了解决方法。仅创建一个
BouncyCastleProvider
的实例似乎不会触发错误。您应该仔细检查您的代码,以确保您没有在某处调用新的BouncyCastleProvider()。顺便说一句,你的侦探工作做得不错。@PresidentJamesK.Polk-确实有这样的地方。谢谢
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"));
}
private static BouncyCastleProvider bouncycastleprovider = null;
public static synchronized BouncyCastleProvider getinstance () {
if (bouncycastleprovider == null) {
bouncycastleprovider = new BouncyCastleProvider();
}
return bouncycastleprovider;
}