Open In App

KeyStore getKey() method in Java with Examples

Last Updated : 10 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The getKey() method of java.security.KeyStore class is used to get the key associated with the given alias, using the given password to recover it.

Syntax:

public final Key getKey(String alias, char[] password)
    throws KeyStoreException, 
           NoSuchAlgorithmException, 
           UnrecoverableKeyException

Parameter: This method accepts following parameters:

  • alias: which is the alias name with whom the key is to be checked
  • password: which is the password for recovering the key that is to be checked

Return Value: This method returns the key for the requested alias if it exists.

Exception: This method throws following exceptions

  • KeyStoreException: if keystore is not loaded.
  • NoSuchAlgorithmException: if the algorithm is missing

Note: All the programs in this article won’t run on online IDE as no ‘privatekey’ Keystore exists. You can check this code on Java compiler on your system. To check this code, create a Keystore ‘privatekey’ on your system and set your own Keystore password to access that Keystore.

Below are the examples to illustrate the getKey() method:

Example 1:




// Java program to demonstrate getKey() method
  
import java.security.*;
import java.security.cert.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // creating the object of KeyStore
            // and getting instance
            // By using getInstance() method
            KeyStore sr = KeyStore.getInstance("JKS");
  
            // keystore password is required to access keystore
            char[] pass = ("123456").toCharArray();
  
            // creating and initializing object of InputStream
            InputStream is
                = new FileInputStream(
                    "f:/java/private key.store");
  
            // initializing keystore object
            sr.load(is, pass);
  
            // getting the Key
            // using getKey() method
            Key key = sr.getKey("ftpkey", pass);
  
            // display the result
            System.out.println("Key : "
                               + key);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (KeyStoreException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (FileNotFoundException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (IOException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (CertificateException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (UnrecoverableKeyException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Example 2: For KeyStoreException




// Java program to demonstrate getKey() method
  
import java.security.*;
import java.security.cert.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // creating the object of KeyStore
            // and getting instance
            // By using getInstance() method
            KeyStore sr = KeyStore.getInstance("JKS");
  
            // keystore password is required to access keystore
            char[] pass = ("123456").toCharArray();
  
            // creating and initializing object of InputStream
            InputStream is
                = new FileInputStream(
                    "f:/java/private key.store");
  
            // initializing keystore object
            // sr.load(is, pass);
  
            // getting the Key
            // using getKey() method
            Key key = sr.getKey("ftpkey", pass);
  
            // display the result
            System.out.println("Key : "
                               + key);
        }
        catch (FileNotFoundException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (KeyStoreException e) {
  
            System.out.println("\nException thrown : " + e);
        }
        catch (UnrecoverableKeyException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Reference: https://docs.oracle.com/javase/9/docs/api/java/security/KeyStore.html#getKey-java.lang.String-char:A-



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads