Listing the Aliases in a Key Store - Java Security

Java examples for Security:Key

Description

Listing the Aliases in a Key Store

Demo Code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

public class Main {
  public static void main(String[] args) {
    try {// www  . ja  v  a2  s.  c  om
      // Load the keystore in the user's home directory
      File file = new File(System.getProperty("user.home") + File.separatorChar
          + ".keystore");
      FileInputStream is = new FileInputStream(file);
      KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
      String password = "my-keystore-password";
      keystore.load(is, password.toCharArray());

      // List the aliases
      Enumeration e = keystore.aliases();
      for (; e.hasMoreElements();) {
        String alias = (String) e.nextElement();

        // Does alias refer to a private key?
        boolean b = keystore.isKeyEntry(alias);

        // Does alias refer to a trusted certificate?
        b = keystore.isCertificateEntry(alias);
      }
      is.close();
    } catch (java.security.cert.CertificateException e) {
    } catch (NoSuchAlgorithmException e) {
    } catch (FileNotFoundException e) {
      // Keystore does not exist
    } catch (KeyStoreException e) {
    } catch (IOException e) {
    }
  }
}
> keytool -list -storepass my-keystore-password

Related Tutorials