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:
Example 2: For
KeyStoreException