当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java KeyPairGenerator getInstance()用法及代码示例


getInstance(String algorithm)

java.security.KeyPairGenerator类的getInstance()方法用于返回KeyPairGenerator对象,该对象为指定算法生成公用/专用 key 对。

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

用法:


public static KeyPairGenerator 
    getInstance(String algorithm)
        throws NoSuchAlgorithmException

参数:此方法将“算法”的标准名称作为参数。

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

异常:如果没有提供者支持指定算法的Signature实现,则此方法将引发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(String algorithm, Provider provider)

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

用法:


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

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

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

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

异常:此方法引发以下异常:

  • NoSuchAlgorithmException –如果指定的Provider对象不提供指定算法的SignatureSpi实现。
  • IllegalArgumentException –如果提供者为null。
  • 下面是说明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
    


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 KeyPairGenerator getInstance() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。