Java中的 KeyStore getCertificateAlias() 方法及示例
Java.security.KeyStore类的getCertificateAlias()方法用于获取具有指定证书的第一个 KeyStore 条目的名称。
句法:
public final String getCertificateAlias(Certificate cert)
throws KeyStoreException
参数:此方法接受证书名称一个参数,该参数将与 Keystore 条目匹配。
返回值:此方法返回具有指定证书的第一个 KeyStore 条目的名称。
异常:如果您不初始化此密钥库,此方法将引发KeyStoreException 。
注意:本文中的所有程序都不会在在线 IDE 上运行,因为不存在“privatekey”密钥库。您可以在系统上的Java编译器上检查此代码。要检查此代码,请在您的系统上创建一个密钥库“privatekey”并设置您自己的密钥库密码以访问该密钥库。
以下是说明getCertificate()方法的示例:
示例 1:
Java
// Java program to demonstrate getCertificate() 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 certificate
X509Certificate cert
= (X509Certificate)sr
.getCertificate("ftpkey");
// getting alias name for the keyentry
// using getCertificateAlias() method
String alias = sr.getCertificateAlias(cert);
// display the result
System.out.println("alias name for the particular entry : "
+ alias);
}
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);
}
}
}
Java
// Java program to demonstrate getCertificateAlias() 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 certificate
X509Certificate cert
= (X509Certificate)sr
.getCertificate("ftpkey");
// getting alias name for the keyentry
// using getCertificateAlias() method
String alias = sr.getCertificateAlias(cert);
// display the result
System.out.println("alias name for the particular entry : "
+ alias);
}
catch (FileNotFoundException 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);
}
}
}
输出:
示例 2:对于KeyStoreException
Java
// Java program to demonstrate getCertificateAlias() 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 certificate
X509Certificate cert
= (X509Certificate)sr
.getCertificate("ftpkey");
// getting alias name for the keyentry
// using getCertificateAlias() method
String alias = sr.getCertificateAlias(cert);
// display the result
System.out.println("alias name for the particular entry : "
+ alias);
}
catch (FileNotFoundException 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);
}
}
}
输出:
参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/KeyStore.html#getCertificateAlias-java.security.cert.Certificate-