java 获取windows系统安装的证书或证书链

 

    有时需要获取windows系统安装的证书或证书链,比如说你要通过证书来创建java的密钥库  。

有关证书链的解释可以查看此处

 

public static void main(String[] args) {
		SunMSCAPI providerMSCAPI = new SunMSCAPI();
		Security.addProvider(providerMSCAPI);
		KeyStore ks;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			ks = KeyStore.getInstance("Windows-MY");
			ks.load(null, null);
		      Enumeration aliases = ks.aliases();
              while (aliases.hasMoreElements()) {
                  String alias = (String)aliases.nextElement();          
                  Certificate[] certs = ks.getCertificateChain(alias);
                  if (certs != null) {
                      System.out.println("证书链:" + alias + "':");
                      for (int c = 0; c < certs.length; c++) {
                          if (certs[c] instanceof X509Certificate) {
                              X509Certificate cert = (X509Certificate)certs[c];
                              System.out.println("  顺序: " + (c + 1) + ":");
                              System.out.println("  主题: " + cert.getSubjectDN());
                              System.out.println("  签名算法: " + cert.getSigAlgName());
                              System.out.println("  签发时间: " + sdf.format(cert.getNotBefore()) );
                              System.out.println("  到期时间: " +sdf.format(cert.getNotAfter()));
                              System.out.println("  签发者: " + cert.getIssuerDN());
                          }
                      }
                  }
              }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

 

你可能感兴趣的:(windows)