Java CertificateFactory

Jakob Jenkov
Last update: 2018-02-14

The Java CertificateFactory class (java.security.cert.CertificateFactory) is capable of creating Java Certificate instances from binary certificate encodings like X.509 (ASN.1 DER). To read more about the Java Certificate class, see the Java Certificate tutorial.

The Java CertificateFactory can also create CertPath instances. A CertPath is a chain of certificates where each certificate in the chain is signed by the next certificate in the chain. See the Java CertPath tutorial for more information about the CertPath class.

Creating a CertificateFactory Instance

Before you can create Certificate instances you must create a Java CertificateFactory instance. Here is an example of creating a CertificateFactory:

 CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

This example creates a CertificateFactory instance capable of creating X.509 certificate instances (X509Certificate - a subclass of Certificate).

Creating a Certificate Instance

Once you have created a CertificateFactory instance you can start creating Certificate instances. You do so via the generateCertificate() method. Here is a generateCertificate() example:

InputStream certificateInputStream = new FileInputStream("my-x509-certificate.crt");

Certificate certificate = certificateFactory.generateCertificate(certificateInputStream);

Creating a CertPath Instance

The Java CertificateFactory can also create a CertPath instance. You create a CertPath instance by calling the CertificateFactory generateCertPath() method. Here is a generateCertPath() example:

InputStream certificateInputStream = new FileInputStream("my-x509-certificate-chain.crt");

CertPath certPath = certificateFactory.generateCertPath(certificateInputStream);

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next