java - 如何在JAVA中获取SSL/TLS证书

标签 java ssl-certificate x509certificate

我需要做什么才能使用 Java 获取存储在 Windows 和 Linux 计算机中的所有 SSL/TLS 证书?

我将构建一个 Java 应用程序,获取计算机中存储的所有 SSL/TLS 证书,并将每个证书保存在一个文件中。

我说的是 Windows keystore 中的 SSL/TLS 证书,您可以通过这些证书查看

certmgr.msc(将其放入 Windows 计算机的搜索栏中)

这是 Google Chrome 和 Internet Explorer 使用的。

最佳答案

已解决,这里是代码中的解决方案:

public class Main {
    private static final String CER_PATH = "**PATH_TO_SAVE_CERTIFICATES**";

    public static void main(String[] args) throws Exception {
        new File(CER_PATH).mkdirs();
        KeyStore ks = KeyStore.getInstance("Windows-ROOT", "SunMSCAPI");
        ks.load(null, null);
        Enumeration<String> en = ks.aliases();
        int n = 0;
        while (en.hasMoreElements()) {
            String aliasKey = en.nextElement();
            Certificate certificate = ks.getCertificate(aliasKey);
            saveCertificate(certificate, n++ + ". " + aliasKey);
        }
    }

    public static void saveCertificate(Certificate certificate, String name) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(CER_PATH + name + ".cer");
            fos.write(certificate.getEncoded());
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (CertificateEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore ... any significant errors should already have been
                    // reported via an IOException from the final flush.
                }
            }
        }
    }
}

关于java - 如何在JAVA中获取SSL/TLS证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32504483/

相关文章:

ssl - ECDSA 证书可以有 RSA 签名吗?

在主域下注册的 SSL,我们可以用于子域吗?

java - 主题备用名称必须具有在 java 中工作的方案?

android - 将 .p12 文件导入 AndroidKeyStore

java - 当存在相同别名时,从 Windows-MY 获取证书

java - 如何在游戏循环中使用 repaint() 方法

Java- Math.random() : Selecting an element of a 13 by 13 triangular array

java - 是否可以跨操作系统跟踪所有可能影响 eclipse 项目的设置/配置/补丁等?

java - 将时间戳字符串解析为Java.sql.timestamp

ssl - 用于使用 SSL Pinning 的移动应用程序的 Charles 代理