如何在Android中读取.crt文件

时间:2015-06-11 06:40:44

标签: android

我编写以下代码来实现读取.crt文件,但它不起作用。

AssetManager assetManager = getAssets();

InputStream in = null;

in = assetManager.open("client.crt");

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

X509Certificate ca = (X509Certificate) cf.generateCertificate(in);

最后一行不执行。有人请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:-1)

试试此代码段

CertificateFactory cf = CertificateFactory.getInstance("X.509");
    // From https://www.washington.edu/itconnect/security/ca/load-der.crt
    InputStream is = context.getResources().getAssets().openAsset("somefolder/somecertificate.crt"); // path should be your files path
    InputStream caInput = new BufferedInputStream(is);
    Certificate ca;
    try {
        ca = cf.generateCertificate(caInput);
        // System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
    } finally {
        caInput.close();
    }