java.security.KeyStore類的getKey()方法用於獲取與給定別名關聯的 key ,並使用給定密碼進行恢複。
用法:
public final Key getKey(String alias, char[] password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException
參數:此方法接受以下參數:
- alias:這是用來檢查 key 的別名
- password:這是用於恢複要檢查的 key 的密碼
返回值:此方法返回所請求別名的 key (如果存在)。
異常:此方法引發以下異常
- KeyStoreException:如果未加載 key 庫。
- NoSuchAlgorithmException:如果缺少算法
注意:由於不存在“ privatekey” key 庫,因此本文中的所有程序都無法在在線IDE上運行。您可以在係統上的Java編譯器上檢查此代碼。要檢查此代碼,請在係統上創建 key 庫“ privatekey”,並設置自己的 key 庫密碼以訪問該 key 庫。
下麵是說明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);
}
}
}
相關用法
- Java AbstractMap.SimpleEntry getKey()用法及代碼示例
- Java KeyStore getCertificate()用法及代碼示例
- Java KeyStore isCertificateEntry()用法及代碼示例
- Java KeyStore containsAlias()用法及代碼示例
- Java KeyStore getProvider()用法及代碼示例
- Java KeyStore getDefaultType()用法及代碼示例
- Java KeyStore getType()用法及代碼示例
- Java KeyStore getCreationDate()用法及代碼示例
- Java KeyStore isKeyEntry()用法及代碼示例
- Java KeyStore getEntry()用法及代碼示例
- Java KeyStore deleteEntry()用法及代碼示例
- Java KeyStore aliases()用法及代碼示例
- Java KeyStore getCertificateChain()用法及代碼示例
- Java KeyStore getCertificateAlias()用法及代碼示例
- Java JavaTuples getKey()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 KeyStore getKey() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。