SSL Retrofit Android:Stream不代表pkcs12密钥库

时间:2017-05-25 06:21:39

标签: java android ssl-certificate retrofit2 android-keystore

我无法加载密钥库。

  • 我想在OkHttpClient Retrofit中添加sslSocketFactory
  • 加载后#34; .p12"从inputstream到keystore.load方法的文件我得到了异常

"方法抛出' java.io.IOException'例外。 stream不代表pkcs12密钥库改造android"。

返回SSLSocketFactory

的方法
public static SSLSocketFactory getSSLSocketFactory() {

    SSLContext sslContext = null;

    try {

        String password = ""; // .p12 is without a password.

        // Load CAs from an InputStream
        InputStream caInput = MainApplication.context.getResources()
        .openRawResource(R.raw.generic_client_cert); // file present in Row directory with "generic_client_cert.p12" name.

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(caInput, password.toCharArray()); 

以上行的例外情况。

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");

        kmf.init(keyStore, password.toCharArray());

        KeyManager[] keyManagers = kmf.getKeyManagers();


        // creating an SSLSocketFactory that uses our TrustManager
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers, null, null);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
    }

    return sslContext.getSocketFactory();

}

出了什么问题?任何帮助都会很棒(^ _ ^)

由于

0 个答案:

没有答案