Open In App

How to Check Certificate Name and Alias in Keystore Files?

Last Updated : 26 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

You just created your app and invested a great amount of time, invested to get your app created, finally, you are just heading to publish your app, but just before you do that, you need to check the certificate name and alias in your Keystore file, so in order to ensure that you are signing the app with the correct key. So, in this article, you will come to know how you would get a solution to this problem.

What is a KeyStore?

Before we begin, you should be knowing what exactly is a Keystore alias. When you build a secret key, a key pair (public and private key), or add a certificate or certificate chain to the list of trusted certificates using the -genseckey commands, you must specify an alias.

Method #1: Using the command line to find the alias and the certificate name

The most efficient way is to utilize the provided console inside the android studio, and simply execute these commands in the terminal:

keytool -v -list -keystore .keystore

If you are searching for something more specific you may add it up in this command:

keytool -list -keystore .keystore -alias foo

Note: If the searched alias is not found anywhere, you will get an error saying:

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

Method #2: Using Java to programmatically find the key alias

You can use your actual app to show the alias name, and put that to use, for that you need to use this code:

Java




try {
        File gfgFile = new File(gfgKeystore location);
        InputStream is = new FileInputStream(gfgFile);
        KeyStore gfgKeystore = KeyStore.getInstance(KeyStore.getDefaultType());
        String password = "password";
        gfgKeystore.load(is, password.toCharArray());
        Enumeration<String> enumeration = gfgKeystore.aliases();
        while(enumeration.hasMoreElements()) {
            String alias = enumeration.nextElement();
            System.out.println("alias name: " + alias);
            Certificate certificate = gfgKeystore.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();
            }
    }


Just run your app, like you usually would, and you will find your alias in the Android Studio’s Logcat.

Method #3: Using third-party applications

The last method is by using a tool similar to the stock Android Studio but dedicated to finding the key’s alias, it’s also worth giving it a shot if you are unable to find your alias from the above 2 methods, You need to download: KeyStore Explorer. This is an Open Source App, and you won’t have to pay anything to use this, just some bare knowledge of the Android Environment would help you get your way!

Conclusion

Those are all the possible ways you can use to find your key’s alias, you can use any one of them and get your work going, and move forward publishing your android App on the Google Play Store.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads