Java中的 KeyStore getKey() 方法及示例
Java.security.KeyStore类的getKey()方法用于获取与给定别名关联的密钥,使用给定的密码来恢复它。
句法:
public final Key getKey(String alias, char[] password)
throws KeyStoreException,
NoSuchAlgorithmException,
UnrecoverableKeyException
参数:此方法接受以下参数:
- alias:用来检查密钥的别名
- 密码:这是恢复要检查的密钥的密码
返回值:此方法返回所请求别名的键(如果存在)。
异常:此方法抛出以下异常
- KeyStoreException :如果未加载密钥库。
- NoSuchAlgorithmException : 如果算法缺失
注意:本文中的所有程序都不会在在线 IDE 上运行,因为不存在“privatekey”密钥库。您可以在系统上的Java编译器上检查此代码。要检查此代码,请在您的系统上创建一个 Keystore 'privatekey' 并设置您自己的 Keystore 密码以访问该 Keystore。
以下是说明getKey()方法的示例:
示例 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);
}
}
}
输出:
示例 2:对于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);
}
}
}
输出:
参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/KeyStore.html#getKey-java.lang.String-char:A-