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


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


getInstance(String algorithm)

java.security.AlgorithmParameterGenerator类的getInstance()方法用于返回实现指定算法的AlgorithmParameterGenerator类型的对象。

用法:

public static AlgorithmParameterGenerator 
  getInstance(String algorithm)
    throws NoSuchAlgorithmException

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


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

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

  • NoSuchAlgorithmException:如果没有提供者支持指定算法的AlgorithmParameterGeneratorSpi实现。
  • NullPointerException :如果指定的算法为null。

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

示例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("DSA"); 
  
            // getting the status of 
            // AlgorithmParameterGenerator object 
            String str = sr.toString(); 
  
            // printing the status 
            System.out.println("Status: "
                               + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
输出:
Status: java.security.AlgorithmParameterGenerator@232204a1

示例2:显示NoSuchAlgorithmException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("GFG"); 
  
            // getting the status of 
            // AlgorithmParameterGenerator object 
            String str = sr.toString(); 
  
            // printing the status 
            System.out.println("Status: " + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
输出:
Exception thrown:
 java.security.NoSuchAlgorithmException:
 GFG AlgorithmParameterGenerator not available

Signature getInstance(String algorithm, Provider provider)

java.security.AlgorithmParameterGenerator类的getInstance()方法用于返回实现指定签名算法和指定提供者对象的AlgorithmParameterGenerator对象。

用法:

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

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


  • algorithm:这是所请求算法的名称。
  • provider:指定的提供者

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

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

  • NoSuchAlgorithmException:如果指定的Provider对象不提供指定算法的AlgorithmParameterGeneratorSpi实现。
  • IllegalArgumentException:如果提供者为null。
  • NullPointerException :如果算法为空

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

示例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("DSA"); 
  
            // creating Provider object 
            Provider pd = sr.getProvider(); 
  
            // getting algorithm name 
            // by using getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr1 
                = AlgorithmParameterGenerator 
                      .getInstance(algo, pd); 
  
            // getting the status of 
            // AlgorithmParameterGenerator object 
            String str = sr1.toString(); 
  
            // printing the status 
            System.out.println("Status: "
                               + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
输出:
Status: java.security.AlgorithmParameterGenerator@232204a1

示例2:显示NoSuchAlgorithmException

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
        try { 
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("GFG"); 
  
            // creating Provider object 
            Provider pd = sr.getProvider(); 
  
            // getting algorithm name 
            // by using getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // Getting instance of 
            // AlgorithmParameterGenerator 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr1 
                = AlgorithmParameterGenerator 
                      .getInstance(algo, pd); 
  
            // getting the status of 
            // AlgorithmParameterGenerator object 
            String str = sr1.toString(); 
  
            // printing the status 
            System.out.println("Status: " + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown: "
                               + e); 
        } 
    } 
}
输出:
Exception thrown:
 java.security.NoSuchAlgorithmException:
 GFG AlgorithmParameterGenerator not available

参考:



相关用法


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