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 class. is a class to read in different types of certificates from certificate files.

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.

 * Copyright (c) All Rights Reserved.
class JcaCertificateTest {
   public static void main(String[] a) {
      if (a.length<2) {
         System.out.println("java JcaCertificateFactoryTest input"
            +" output");
      String input = a[0];
      String output = a[1];
      try {
      } catch (Exception e) {
         System.out.println("Exception: "+e);
   private static void test(String input, String output)
      throws Exception {
      CertificateFactory cf = CertificateFactory.getInstance("X.509");
      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); cert = cf.generateCertificate(fis);
      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("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();

See the next section for test result of

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 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

 Java Logging

 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 - The Certificate Class

Using CertificateFactory Class to Read in Certificates

 Reading and Writing Certificates in DER and RFC Formats - The 'keystore' Class - 'keystore' Class Test Program

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 Full Version in PDF/EPUB