使用Bouncy Castle后,Android上没有系统TLS

问题描述

我正在使用Bouncy Castle Security Provider进行加密/解密。 我还要确保在每次Bouncy城​​堡通话后都删除BC提供程序。 这是BC代码

public static boolean pubVerifySign(
        String pKey,String sSignature,String sChallenge) throws UnsupportedEncodingException{
    BouncyCastleProvider bcp = initSecuritySubsystem();
    
    boolean bOutput = false;
    try{
        EcpublicKey ecpublicKey = 
                getPublicKeyForBytes(Hex.decode(pKey));
    
        bOutput =
                pubVerifySign(ecpublicKey,sSignature,sChallenge);
    }finally{
        Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
    }
    return bOutput;
}

但是,紧接代码之后。 但是,每当我尝试使用URL.openConnection或任何其他Http库时, 我收到以下错误:没有系统TLS:

cz.msebera.android.httpclient.ssl.SSLInitializationException: java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore BKS implementation not found
at cz.msebera.android.httpclient.ssl.SSLContexts.createDefault(SSLContexts.java:57)
at cz.msebera.android.httpclient.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:978)
at cz.msebera.android.httpclient.impl.client.HttpClients.createDefault(HttpClients.java:56)
at org.nebucoin.user.utils.URLUtils.getHttp(URLUtils.java:63)Caused by: java.security.KeyManagementException: java.security.KeyStoreException:       java.security.NoSuchAlgorithmException: KeyStore BKS implementation not found
at org.conscrypt.SSLParametersImpl.createDefaultX509KeyManager(SSLParametersImpl.java:534)
at org.conscrypt.SSLParametersImpl.getDefaultX509KeyManager(SSLParametersImpl.java:515)
at org.conscrypt.SSLParametersImpl.<init>(SSLParametersImpl.java:126)
at org.conscrypt.OpenSSLContextImpl.engineInit(OpenSSLContextImpl.java:104)
at javax.net.ssl.SSLContext.init(SSLContext.java:349)
at cz.msebera.android.httpclient.ssl.SSLContexts.createDefault(SSLContexts.java:52)

我尝试了无数其他Http Android库(OkHTTP,Apache HTTP Client等),仍然遇到相同的错误。 我甚至向该项目添加了conscypt官方Android库,并像这样将Conscrypt设置为第一个提供程序:

Security.insertProviderAt(Conscrypt.newProvider(),1);

没有任何运气。

任何想法都会受到欢迎!

注意:当我在基于BC /基于pubVerifySign之前使用URL.openConnection时,一切都很好!

解决方法

BouncyCastle有两个提供程序,您可能需要将两者都删除。

    Security.insertProviderAt(BouncyCastleProvider(),1)
    Security.insertProviderAt(BouncyCastleJsseProvider(),2)

如果JSSE提供程序仍然存在,则它将在找不到所需的密钥库时失败。