KeyStore isCertificateEntry() method in Java, examples
Original text: https://www . geesforgeks . org/keystore-iscertifientry-method-in-Java-with-examples/
The isCertificateEntry() method of the java.security.KeyStore class is used to check whether the specified certificate entry exists in this KeyStore instance. It returns a boolean value representing the same.
Syntax:
public final boolean isCertificateEntry(String alias)
throws KeyStoreException
Parameters:This method accepts alias The name of the parameter whose certificate entry is to be checked.
Return Value: This method checks whether the certificate entry for the requested alias exists and returns a Boolean value.
Exception: If the keystore has not been initialized, this method throws KeystoreException.
Note: None of the programs in this article will run on the online IDE because the “privatekey” keystore does not exist. You can inspect this code on your system’s Java compiler. To check this code, create a keystore “privatekey” on your system and set your own keystore password to access the keystore.
The following is an example illustrating the iscertificientry() method:
Example 1:
// 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
span>
KeyStore sr = KeyStore.getInstance("JKS");
// keystore password is required to access keystore
char[] pass = ("123456").toCharArray();
// creating and initializing object of InputStream
>new FileInputStream(
key.store");
// initializing keystore object
sr.load(is, pass);
class="n">Boolean status
= sr.isCertificateEntry( "ftpkey");
// display the result
if (statusclass 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
"o">= new FileInputStream(
/ initializing keystore object
// sr.load(is, pass);
// checking the presence of certificate entry
// using isCertificateEntry() method
Boolean status
= sr. isCertificateEntry("ftpkey") ;
// display the result
if ( status)
"p">.out.println(
"\nCertificate "
+ "entry is present");
"n">System.out.println(
"o">+ "entry is not present");
}
catch (NullPointerException e) {
System .out.println("Exception thrown : " + e);
}
catch (KeyStoreException e) {
span>out.println( "Exception thrown : " + e);
}
catch (FileNotFoundException e) {
System.out .println("Exception thrown : " + e);
}
}
}
Output:
Reference:https://docs.Oracle.com/javase/9/docs/API/Java/security/keystore. html # iscertifieteentry-Java . lang . string-
pan class=”na”>println(“Exception thrown : “ + e);
}
}
}
Output:
Reference: https://docs . Oracle . com/javase/9/docs/API/Java/security/keystore . html #iscertifieteentry-Java . lang . string-