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






Getting the Subject and Issuer Distinguished Names of an X509 Certificate

   

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.Principal;
import java.security.cert.X509Certificate;
import java.util.Enumeration;

public class Main {
  public static void main(String[] argv) throws Exception {
    FileInputStream is = new FileInputStream("your.keystore");

    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(is, "my-keystore-password".toCharArray());

    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();
      }
    }
  }
}

   
    
    
  








Related examples in the same category

1.Signature Test
2.Specify the keystore of certificates using the javax.net.ssl.keyStore system property:
3.Retrieving a Certificate from a Key Store
4.Adding a Certificate to a Key Store
5.Creating a Certification Path
6.Listing the Most-Trusted Certificate Authorities (CA) in a Key Store
7.Validating a Certification Path using the most-trusted CAs in the JDK's cacerts file.
8.Importing a Certificate from a File
9.Retrieving the Certification Path of an SSL Server
10.Creates a CertStore from the contents of a file-system directory.