Store Certificate : Certificate « Security « Java Tutorial






import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.security.KeyStore;
import java.security.cert.CertPath;
import java.security.cert.X509Certificate;
import java.util.List;

public class MainClass {
  public static void main(String args[]) throws Exception {
    FileInputStream f = new FileInputStream("CertPath.dat");
    ObjectInputStream b = new ObjectInputStream(f);
    CertPath cp = (CertPath) b.readObject();

    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, null);
    List cplist = cp.getCertificates();
    Object[] o = cplist.toArray();
    for (int i = 0; i < o.length; i++) {
      X509Certificate c = (X509Certificate) o[i];
      ks.setCertificateEntry("my" + i, c);
    }
    FileOutputStream output = new FileOutputStream("MyCertPathStore");
    ks.store(output, "mypass".toCharArray());
    output.close();

  }
}








36.7.Certificate
36.7.1.Using Certificates in Java
36.7.2.KeyStore Example
36.7.3.Creating a Certificate in Java
36.7.4.Import certificate
36.7.5.Generate X.509 certificate
36.7.6.Store Certificate
36.7.7.Validate certificate
36.7.8.Exporting a Certificate to a File
36.7.9.Validating a Certification Path using the most-trusted CAs in the JDK's cacerts file.