在Android中加载服务器证书

时间:2018-08-13 03:29:58

标签: java android openssl ssl-certificate x509certificate

我正在尝试在android中加载证书。这是我的代码,

InputStream rootCert = getResources().openRawResource(R.raw.cert);

CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate cert = certFactory.generateCertificate(rootCert);

但是不知道为什么我遇到以下错误,

08-12 22:04:00.814 24520-24520/com.example.user.ssl E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.user.ssl, PID: 24520
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.ssl/com.example.user.ssl.MainActivity}: java.lang.NullPointerException: asset
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
     Caused by: java.lang.NullPointerException: asset
        at android.content.res.AssetManager.seekAsset(Native Method)
        at android.content.res.AssetManager.-wrap4(AssetManager.java)
        at android.content.res.AssetManager$AssetInputStream.mark(AssetManager.java:684)
        at com.android.org.conscrypt.OpenSSLX509CertificateFactory$Parser.generateItem(OpenSSLX509CertificateFactory.java:72)
        at com.android.org.conscrypt.OpenSSLX509CertificateFactory.engineGenerateCertificate(OpenSSLX509CertificateFactory.java:272)
        at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:362)
        at com.example.user.ssl.MainActivity.onCreate(MainActivity.java:101)
        at android.app.Activity.performCreate(Activity.java:6956)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)

有人可以帮助解决这个问题吗?

0 个答案:

没有答案