How to check certificate name and alias in keystore files? How to check certificate name and alias in keystore files? java java

How to check certificate name and alias in keystore files?


You can run the following command to list the content of your keystore file (and alias name):

keytool -v -list -keystore .keystore

If you are looking for a specific alias, you can also specify it in the command:

keytool -list -keystore .keystore -alias foo

If the alias is not found, it will display an exception:

keytool error: java.lang.Exception: Alias does not exist


In order to get all the details I had to add the -v option to romaintaz answer:

keytool -v -list -keystore <FileName>.keystore


You can run from Java code.

try {        File file = new File(keystore location);        InputStream is = new FileInputStream(file);        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());        String password = "password";        keystore.load(is, password.toCharArray());        Enumeration<String> enumeration = keystore.aliases();        while(enumeration.hasMoreElements()) {            String alias = enumeration.nextElement();            System.out.println("alias name: " + alias);            Certificate certificate = keystore.getCertificate(alias);            System.out.println(certificate.toString());        }    } catch (java.security.cert.CertificateException e) {        e.printStackTrace();    } catch (NoSuchAlgorithmException e) {        e.printStackTrace();    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (KeyStoreException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }finally {        if(null != is)            try {                is.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }    }

Certificate class holds all information about the keystore.

UPDATE- OBTAIN PRIVATE KEY

Key key = keyStore.getKey(alias, password.toCharArray());String encodedKey = new Base64Encoder().encode(key.getEncoded());System.out.println("key ? " + encodedKey);

@prateek Hope this is what you looking for!