Getting the Subject and Issuer Distinguished Names of an X509 Certificate - Java Security

Java examples for Security:Certificate

Description

Getting the Subject and Issuer Distinguished Names of an X509 Certificate

Demo Code

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.Principal;
import java.security.cert.X509Certificate;
import java.util.Enumeration;

public class Main {
  public static void main(String[] args) {
    try {/*from w  w  w  .  j  av a  2 s .  c  o m*/
      KeyStore keystore = null;
      Enumeration e = keystore.aliases();
      for (; e.hasMoreElements(); ) {
          String alias = (String)e.nextElement();

          java.security.cert.Certificate cert = keystore.getCertificate(alias);
          if (cert instanceof X509Certificate) {
              X509Certificate x509cert = (X509Certificate)cert;

              // Get subject
              Principal principal = x509cert.getSubjectDN();
              String subjectDn = principal.getName();

              // Get issuer
              principal = x509cert.getIssuerDN();
              String issuerDn = principal.getName();
          }
      }
  } catch (KeyStoreException e) {
  }
  }
}

Related Tutorials