有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的安卓平台独立信任存储路径

我正在尝试用Java加载系统信任存储。问题是,我的代码将在一个库中发布,该库将由Android、Windows、linux和OSX的应用程序使用,并且cacerts文件在每个系统上的位置不同

这是我的密码:

        // Load the JDK's cacerts keystore file.
        String filename = System.getProperty("javax.net.ssl.trustStore");
        if (filename == null)
            filename = System.getProperty("java.home") + "/lib/security/cacerts".replace('/', File.separatorChar);
        FileInputStream is = new FileInputStream(filename);
        // Load the root certificate authorities. Despite the name, KeyStore can also hold certificates.
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        // Yes this is really the password.
        String password = "changeit";
        keystore.load(is, password.toCharArray());
        // Retrieves the most-trusted CAs from keystore.
        PKIXParameters params = new PKIXParameters(keystore);

在linux上进行测试时,这很好,但我认为这在Android上不起作用

是否有一种简单的方法可以通过编程找到系统信任存储的位置,或者我必须明确列举每种可能性,并为每种可能性硬编码信任存储路径


共 (1) 个答案

  1. # 1 楼答案

    呼吁:

    KeyStore keystore = KeyStoreUtil.getCacertsKeyStore();
    

    将返回所有系统CA可信证书,这是一种独立于平台的读取代码文件的方法

    备注:如果使用空密码,代码将正常工作:

    String password = null;//"changeit";