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

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

Java中的 KeyPairGenerator getInstance() 方法及示例

getInstance(字符串算法)

Java.security.KeyPairGenerator类的getInstance()方法用于返回一个 KeyPairGenerator 对象,该对象为指定的算法生成公钥/私钥对。

此方法遍历已注册的安全提供者列表,从最喜欢的提供者开始。返回一个新的 KeyPairGenerator 对象,该对象封装来自支持指定算法的第一个 Provider 的 KeyPairGeneratorSpi 实现。

句法:

public static KeyPairGenerator 
    getInstance(String algorithm)
        throws NoSuchAlgorithmException

参数:该方法以算法的标准名称为参数。

返回值:此方法返回新的 KeyPairGenerator 对象。

异常:如果没有提供者支持指定算法的签名实现,则此方法抛出NoSuchAlgorithmException

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

示例 1:

// Java program to demonstrate
// getInstance() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of
            // KeyPairGenerator
            // and getting instance
            // using getInstance() method
            KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA");
  
            // getting the Algorithm
            String algo = sr.getAlgorithm();
  
            // printing the algo String
            System.out.println("Algorithm : " + algo);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Algorithm : DSA

示例 2:显示 NoSuchAlgorithmException

// Java program to demonstrate
// getInstance() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of
            // KeyPairGenerator
            // and getting instance
            // using getInstance() method
  
            System.out.println("Trying to get"
                               + " the instance of unknown Algorithm");
  
            KeyPairGenerator sr = KeyPairGenerator
                                      .getInstance("TAJMAHAL");
  
            // getting the Algorithm
            String algo = sr.getAlgorithm();
  
            // printing the algo String
            System.out.println("Algorithm : " + algo);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Trying to get the instance of unknown Algorithm
Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL KeyPairGenerator not available

getInstance(字符串算法,Provider提供者)

Java.security.KeyPairGenerator 类的getInstance()方法用于返回一个 KeyPairGenerator 对象,该对象为指定的算法生成公钥/私钥对。
返回一个新的 KeyPairGenerator 对象,该对象封装了来自指定 Provider 对象的 KeyPairGeneratorSpi 实现。请注意,指定的提供者对象不必在提供者列表中注册。

句法:

public static KeyPairGenerator 
    getInstance(String algorithm, Provider provider)
        throws NoSuchAlgorithmException

参数:此方法将以下参数作为参数:

  • algorithm :请求的算法的名称。
  • 提供者:提供者

返回值:此方法返回新的 KeyPairGenerator 对象。

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

  • NoSuchAlgorithmException – 如果指定算法的 SignatureSpi 实现不能从指定的 Provider 对象获得。
  • IllegalArgumentException–如果提供者为空。

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

    注意:以下程序不会在在线 IDE 上运行。

    示例 1:

    // Java program to demonstrate
    // getInstance() method
      
    import java.security.*;
    import java.util.*;
      
    public class GFG1 {
        public static void main(String[] argv)
        {
            try {
                // creating the object of
                // KeyPairGenerator
                // and getting instance
                // using getInstance() method
                KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA");
      
                // creating Provider object
                Provider pd = sr.getProvider();
      
                // getting algorithm name
                // by using getAlgorithm() mathod
                String algo = sr.getAlgorithm();
      
                // creating the object of KeyPairGenerator and getting instance
                // By usng getInstance() method
                KeyPairGenerator sr1 = KeyPairGenerator.getInstance(algo, pd);
      
                // getting the status of signature object
                KeyPair keypair = sr1.generateKeyPair();
      
                // printing the keypair
                System.out.println("Keypair : " + keypair);
            }
      
            catch (NoSuchAlgorithmException e) {
      
                System.out.println("Exception thrown : " + e);
            }
            catch (ProviderException e) {
      
                System.out.println("Exception thrown : " + e);
            }
        }
    }
    
    输出:
    Keypair : java.security.KeyPair@12a3a380
    

    示例 2:显示 NoSuchAlgorithmException

    // Java program to demonstrate
    // getInstance() method
      
    import java.security.*;
    import java.util.*;
      
    public class GFG1 {
        public static void main(String[] argv)
        {
            try {
                // creating the object of
                // KeyPairGenerator
                // and getting instance
                // using getInstance() method
                KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA");
      
                // creating Provider object
                Provider pd = sr.getProvider();
      
                // creating the object of KeyPairGenerator and getting instance
                // By usng getInstance() method
                KeyPairGenerator sr1 = KeyPairGenerator.getInstance("TAJMAHAL", pd);
      
                // getting the status of signature object
                KeyPair keypair = sr1.generateKeyPair();
      
                // printing the keypair
                System.out.println("Keypair : " + keypair);
            }
      
            catch (NoSuchAlgorithmException e) {
      
                System.out.println("Exception thrown : " + e);
            }
            catch (ProviderException e) {
      
                System.out.println("Exception thrown : " + e);
            }
        }
    }
    
    输出:
    Exception thrown : java.security.NoSuchAlgorithmException: 
    no such algorithm: TAJMAHAL for provider SUN
    

    示例 3:显示 IllegalArgumentException

    // Java program to demonstrate
    // getInstance() method
      
    import java.security.*;
    import java.util.*;
      
    public class GFG1 {
        public static void main(String[] argv)
        {
            try {
                // creating the object of
                // KeyPairGenerator
                // and getting instance
                // using getInstance() method
                KeyPairGenerator sr = KeyPairGenerator
                                          .getInstance("RSA");
      
                // creating Provider object
                System.out.println("Trying to assign null as a provider");
                Provider pd = null;
      
                // getting algorithm name
                // by using     getAlgorithm() mathod
                String algo = sr.getAlgorithm();
      
                // creating the object of KeyPairGenerator and getting instance
                // By usng getInstance() method
                KeyPairGenerator sr1 = KeyPairGenerator
                                           .getInstance(algo, pd);
      
                // getting the status of signature object
                KeyPair keypair = sr1.generateKeyPair();
      
                // printing the keypair
                System.out.println("Keypair : " + keypair);
            }
      
            catch (NoSuchAlgorithmException e) {
      
                System.out.println("Exception thrown : " + e);
            }
            catch (ProviderException e) {
      
                System.out.println("Exception thrown : " + e);
            }
            catch (IllegalArgumentException e) {
      
                System.out.println("Exception thrown : " + e);
            }
        }
    }
    
    输出:
    Trying to assign null as a provider
    Exception thrown : java.lang.IllegalArgumentException:
     missing provider