我需要做什么才能使用 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/