KeyStore getEntry() method in Java with Examples
The getEntry() method of java.security.KeyStore class is used to get the keystore entry for this instance with the help of the specified alias and the protection parameter.
Syntax:
public final KeyStore.Entry getEntry(String alias, KeyStore.ProtectionParameter protParam) throws NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException
Parameter: This method accepts following arguments as a parameter.
- alias: which is name of the Keystore Entry to be fetched.
- protParam: which contains the password to access keystore.
Return Value: This method returns the keystore entry for the requested alias if it exists.
Exception: This method throws following exception
- NullPointerException: for null alias.
- NoSuchAlgorithmException: if the algorithm is missing.
- UnrecoverableEntryException: if the specified password is invalid.
- KeyStoreException: if the keystore has not been initialized (loaded).
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 getEntry() method:
Example 1:
Java
// Java program to demonstrate getEntry() 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 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); // creating and initializing // KeyStore.ProtectionParameter object KeyStore.ProtectionParameter entryPassword = new KeyStore.PasswordProtection(pass); // getting KeyStore.PrivateKeyEntry object // using getEntry() method KeyStore.PrivateKeyEntry print = (KeyStore.PrivateKeyEntry)sr .getEntry("ftpkey", entryPassword); // display the result System.out.println("PrivateKey of particular entry: " + print.getPrivateKey()); } 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 (UnrecoverableEntryException e) { System.out.println("Exception thrown : " + e); } } } |
Example 2: For KeyStoreException
Java
// Java program to demonstrate getEntry() 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 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); // creating and initializing // KeyStore.ProtectionParameter object KeyStore.ProtectionParameter entryPassword = new KeyStore.PasswordProtection(pass); // getting KeyStore.PrivateKeyEntry object // using getEntry() method KeyStore.PrivateKeyEntry print = (KeyStore.PrivateKeyEntry)sr .getEntry("ftpkey", entryPassword); // display the result System.out.println( "PrivateKey of particular entry : " + print.getPrivateKey()); } 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 (UnrecoverableEntryException e) { System.out.println("Exception thrown : " + e); } catch (NoSuchAlgorithmException e) { System.out.println("Exception thrown : " + e); } } } |
Reference: https://docs.oracle.com/javase/9/docs/api/java/security/KeyStore.html#getEntry-java.lang.String-java.security.KeyStore.ProtectionParameter-