Home  >  Core Java

Example of CertPath in Java

By Arvind Rai, January 24, 2013
1. CertPath belongs to the package java.security.cert.
2. CertPath handles different kind of certificates like X.509, PGP etc.
3. CertPath is immutable.
4. Each CertPath must support one or more than one encodings like PKCS#7.
5. CertPath objects are Serializable.
6. CertPath object can be created by CertificateFactory. It can also be created by CertPathBuilder.
package com.concretepage.security;

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.Iterator;
import java.util.List;

	   public class CertPathTest {

		     public static void main(String args[]) throws Exception {
		       CertificateFactory cf = CertificateFactory.getInstance("X.509");
		       List<Certificate> list = new ArrayList<Certificate>();
		     
		         FileInputStream in = new FileInputStream(args[0]);
		         Certificate c = cf.generateCertificate(in);
		         list.add(c);
		     
		       CertPath cp = cf.generateCertPath(list);
		       Iterator<String> itr=cp.getEncodings();
		       while(itr.hasNext()){
		    	   System.out.println(itr.next());
		       }
		       List<?> clist = cp.getCertificates();
		       Object[] ob = clist.toArray();
		       for (int i = 0; i < ob.length; i++) {
		         X509Certificate xc = (X509Certificate) ob[i];
		         System.out.println(xc.getSubjectDN());
		         System.out.println(xc.getIssuerDN());
		         System.out.println(xc.getSigAlgName());
		       }
		     }
	  }
In the above example, pass input as argument. It should be a file name and that file should contain X509 encodings.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us