📌  相关文章
📜  Java中的 KeyStore getEntry() 方法及示例

📅  最后修改于: 2022-05-13 01:55:33.963000             🧑  作者: Mango

Java中的 KeyStore getEntry() 方法及示例

Java.security.KeyStore类的getEntry()方法用于在指定别名和保护参数的帮助下获取此实例的密钥库条目。

句法:

public final KeyStore.Entry
  getEntry(String alias, 
           KeyStore.ProtectionParameter protParam)
    throws NoSuchAlgorithmException, 
           UnrecoverableEntryException, 
           KeyStoreException

参数:此方法接受以下参数作为参数。

  • alias :这是要获取的 Keystore 条目的名称。
  • protParam:其中包含访问密钥库的密码。

返回值:此方法返回所请求别名的密钥库条目(如果存在)。

异常:此方法抛出以下异常

  • NullPointerException:用于空别名。
  • NoSuchAlgorithmException:如果缺少算法。
  • UnrecoverableEntryException:如果指定的密码无效。
  • KeyStoreException:如果密钥库尚未初始化(加载)。

注意:本文中的所有程序都不会在在线 IDE 上运行,因为不存在“privatekey”密钥库。您可以在系统上的Java编译器上检查此代码。要检查此代码,请在您的系统上创建一个 Keystore 'privatekey' 并设置您自己的 Keystore 密码以访问该 Keystore。

以下是说明getCertificate()方法的示例:

示例 1:

// 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);
        }
    }
}
输出:

示例 2:对于KeyStoreException

// 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);
        }
    }
}