Generate cert path for X.509 : X509Certificate « Security « Java Tutorial






import java.io.FileInputStream;
import java.security.cert.CertPath;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

public class MainClass {

  public static void main(String args[]) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    List mylist = new ArrayList();
    for (int i = 0; i < args.length; i++) {
      FileInputStream in = new FileInputStream(args[i]);
      Certificate c = cf.generateCertificate(in);
      mylist.add(c);
    }
    CertPath cp = cf.generateCertPath(mylist);
    List cplist = cp.getCertificates();
    Object[] o = cplist.toArray();
    for (int i = 0; i < o.length; i++) {
      X509Certificate c = (X509Certificate) o[i];
      System.out.println(c.getSubjectDN());
      byte[] pbk = c.getPublicKey().getEncoded();
      for (int j = 0; j < pbk.length; j++) {
        System.out.print(pbk[j] + ",");
      }
      System.out.println("\nIssued by " + c.getIssuerDN());
    }
  }
}








36.47.X509Certificate
36.47.1.Print out X509Certificate's properties
36.47.2.Creating a Self-Signed Version 1 Certificate
36.47.3.Creating a Self-Signed Version 3 Certificate
36.47.4.Use X.509 certificate
36.47.5.Display properties of X509 Certificate
36.47.6.Generate cert path for X.509