bouncycastle.jce.provider

问题描述

我正在使用bouncycastle.jce.provider运行Java(openjdk:11.0.8)应用程序:

group: 'org.bouncycastle',name: 'bcprov-jdk15on',version: '1.65'

我注意到内存泄漏,转储显示几乎所有内存都由

组成

javax.crypto.JceSecurity -- > java.util.IdentityHashMap

它是这样的:

enter image description here

似乎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"));
}

解决方法

我遇到了同样的问题,我可以用这个小代码来解决它。我希望这可以帮助某人 :) .

private static BouncyCastleProvider bouncycastleprovider = null;

public static synchronized BouncyCastleProvider getinstance () {

    if (bouncycastleprovider == null) {

        bouncycastleprovider = new BouncyCastleProvider();

    }
    return bouncycastleprovider;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...