如何从X509Certificate字符串创建和填充Keystore对象

时间:2013-05-07 17:12:41

标签: java android x509certificate keystore

我有一个我正在传递的字符串,它表示由:

生成的X509Certificate
String strMessage = 
      Base64.encodeToString(chain[0].getEncoded(), android.util.Base64.NO_WRAP);

这样用户就可以查看证书并可能接受证书。如果他们决定接受它,我需要创建一个Keystore对象,将其添加到我的自定义SSL工厂(如下所示:)

private SSLSocketFactory newSslSocketFactory(boolean trustCert, String cert) {
    try {
        KeyStore trusted = KeyStore.getInstance("BKS");
        if(trustCert) {
            String cert_begin = "-----BEGIN CERTIFICATE-----\n";
            String end_cert = "-----END CERTIFICATE-----";
            String strCert = cert_begin + cert + end_cert;

            byte[] bytes = Base64.decode(cert.getBytes(), Base64.DEFAULT);                    
            InputStream bis = new ByteArrayInputStream(bytes);
            try {
                trusted.load(bis, "password".toCharArray());
            }
            catch(Exception e) {
                int j;
                j = 10;
            }
            finally {
                bis.close();
            }
        }
        AdditionalKeyStoresSSLSocketFactory af = new AdditionalKeyStoresSSLSocketFactory(trusted, null, mTrustCert, mCert);
        af.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        return af;
    } 
    catch (Exception e)  {
        throw new AssertionError(e);
    }
}

但这似乎不起作用。当我遍历我的列表时,它找不到它 有任何想法吗? /洛伦

0 个答案:

没有答案