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:
Java
Example 2: For KeyStoreException
Java
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
- KeyStoreException: if keystore is not loaded.
- NoSuchAlgorithmException: if the algorithm is missing
// 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);
}
}
}
// 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);
}
}
}

