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

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

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

Java.security.KeyStore类的getDefaultType()方法用于提供Keystore 类的默认实例。

句法:

public static final String getDefaultType()

参数:此方法不接受任何参数。
返回值:该方法返回Keystore 类的默认实例

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

下面是说明getDefaultType()方法的示例:

示例 1:

Java
// Java program to demonstrate getDefaultType() 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 type of default keystore
            // using getDefaultType() method
            String type
                = sr.getDefaultType();
 
            // display the result
            System.out.println(
                "type of default"
                + " keystore entry : "
                + type);
        }
 
        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 getDefaultType() 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 type of default keystore
            // using getDefaultType() method
            String type
                = sr.getDefaultType();
 
            // display the result
            System.out.println(
                "type of default keystore entry : "
                + type);
        }
        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:不加载密钥库

Java

// Java program to demonstrate getDefaultType() 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 type of default keystore
            // using getDefaultType() method
            String type
                = sr.getDefaultType();
 
            // display the result
            System.out.println(
                "type of default keystore entry : "
                + type);
        }
        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#getDefaultType–