JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

Using CertificateFactory Class to Read in Certificates

This section provides a tutorial example on how to write a sample program to read in certificate files with the java.security.cert.CertificateFactory class.

java.security.cert.CertificateFactory is a class to read in different types of certificates from certificate files.

  • getIntance() - Returns a CertificateFactory object of the specified certificate type using the implementation of the specified security package provider. If not specified, the default provider will used.
  • generateCertificate() - Read in a certificate from the specified input stream, and returns it as a Certificate object. It can read certificate in both binary (DER encoded) and printable (RFC standard) formats. Note that only the first certificate in the input stream will be processed.
  • generateCertificates() - Read in multiple certificates from the specified input stream, and returns them as a Certificate array. It can read certificate in both binary (DER encoded) and printable (RFC standard) formats.
  • getType() - Returns the type of this certificate factory.
  • getProvider() - Returns the security package provider.

In the following sample program, I want to show you how to read in a certificate from a certificate file, and write it out to a certificate file in the default encoding format.

/**
 * JcaCertificateTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.io.*;
import java.security.*;
import java.security.cert.*;
class JcaCertificateTest {
   public static void main(String[] a) {
      if (a.length<2) {
         System.out.println("Usage:");
         System.out.println("java JcaCertificateFactoryTest input"
            +" output");
         return;
      }
      String input = a[0];
      String output = a[1];
      try {
         test(input,output);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
         return;
      }
   }
   private static void test(String input, String output)
      throws Exception {
      CertificateFactory cf = CertificateFactory.getInstance("X.509");
      System.out.println();
      System.out.println("CertificateFactory Object Info: ");
      System.out.println("Type = "+cf.getType());
      System.out.println("Provider = "+cf.getProvider());
      System.out.println("toString = "+cf.toString());

      FileInputStream fis = new FileInputStream(input);
      java.security.cert.Certificate cert = cf.generateCertificate(fis);
      fis.close();
      System.out.println();
      System.out.println("Certificate Object Info: ");
      System.out.println("Type = "+cert.getType());
      System.out.println("toString = "+cert.toString());

      PublicKey pubKey = cert.getPublicKey();
      System.out.println();
      System.out.println("PublicKey Object Info: ");
      System.out.println("Algorithm = "+pubKey.getAlgorithm());
      System.out.println("Format = "+pubKey.getFormat());
      System.out.println("toString = "+pubKey.toString());
      
      FileOutputStream fos = new FileOutputStream(output);
      byte[] certBytes = cert.getEncoded();
      fos.write(certBytes);
      fos.close();
   }
}

See the next section for test result of JcaCertificateTest.java.

Last update: 2006.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.3.1 on Windows

 Downloading and Installing JDK 1.4.1 on Windows

 Downloading and Installing JDK 1.5.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

KeyStore and Certificate Classes

 java.security.cert.Certificate - The Certificate Class

Using CertificateFactory Class to Read in Certificates

 Reading and Writing Certificates in DER and RFC Formats

 java.security.KeyStore - The 'keystore' Class

 JcaKeyStoreTest.java - 'keystore' Class Test Program

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
Using CertificateFactory Class to Read in Certificates