尝试在Android中使用自签名证书的例外情况

时间:2014-11-30 22:16:35

标签: java android ssl self-signed

我正在尝试加载&使用来自文件的自签名,动态生成的PEM(或DER)X.509证书,并使用该证书通过HTTPClient或HttpsURLConnection加载内容,以便将其注入WebView。 我已经搜索了如何使用带有自定义证书的WebView,但显然只有完全忽略SSL错误的可能性(这不是我想要做的 - 我正在寻找证书验证)。

沿着这个Android developers training的行编码我总是从关键函数中得到例外:TrustManagerFactory,KeyStore和CertificateFactory。


对于TrustManagerFactory:

String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);

-> Unhandled exception type NoSuchAlgorithmException in getInstance()

对于KeyStore,我尝试了教程中的语法以及文档中描述的语法:

String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);

-> Unhandled exception type KeyStoreException in getInstance()

 //Init with empty KeyStore
 KeyStore keyStoreData;
 //Use no protection for key store since it contains public cert
 KeyStore.ProtectionParameter keyStoreProtect = null;
 //Use default type
 String keyStoreType = KeyStore.getDefaultType();


KeyStore.Builder builder = KeyStore.Builder.newInstance(keyStoreType, null, keyStoreProtect);
keyStoreData = builder.getKeyStore();

-> Unhandled exception type KeyStoreException in getKeyStore()

对于CertificateFactory:

CertificateFactory cf = CertificateFactory.getInstance("X.509");

-> Unhandled exception type CertificateException

总之,我不能使用作业所需的任何功能,因为即使我使用的是默认教程代码(根据文档仍然有效),它们都会因异常而失败。

切换IDE(尝试Android Studio和Eclipse with ADT),从OpenJDK切换到Oracle JDK,似乎没有任何帮助。 此外,StackOverflow和网络上的类似问题都通过使用这些功能中的一个或多个来回答 - 所以他们不帮助我。

如果有人知道我做错了什么,我会很高兴得到任何帮助!

0 个答案:

没有答案
相关问题