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


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


getInstance(String algorithm)

java.security.AlgorithmParameters类的getInstance()方法返回应用分配的AlgorithmParameters算法的AlgorithmParameters类型的对象。

用法:

public static AlgorithmParameters
  getInstance(String algorithm)
  throws NoSuchAlgorithmException

参数:此方法将标准算法作为参数。


返回值:该方法提供了一个新的算法参数对象。

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

  • NoSuchAlgorithmException:–如果没有提供者可支持特定算法的算法参数spi应用程序。
  • 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 { 
            // creating the object of AlgorithmParameters 
            // and getting instance 
            // By usng getInstance() method 
            AlgorithmParameters sr 
                = AlgorithmParameters 
                      .getInstance("DES"); 
  
            // getting the status 
            // of AlgorithmParameters 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 : null

示例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 AlgorithmParameters 
            // and getting instance 
            // By usng getInstance() method 
            AlgorithmParameters sr 
                = AlgorithmParameters.getInstance("GFG"); 
  
            // getting the status 
            // of AlgorithmParameters 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 AlgorithmParameters not available

getInstance(String algorithm, Provider provider)

java.security.AlgorithmParameters类的getInstance()方法返回一个AlgorithmParameters类型的对象,该对象应用分配的algorithmParameters算法和分配的提供者对象。

用法:

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

参数:此方法寻求以下参数作为参数:


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

返回值:该方法提供了一个新的算法参数对象。

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

  • NoSuchAlgorithmException:如果没有提供者可支持特定算法的算法参数spi应用程序。
  • 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 { 
            // creating AlgorithmParameterGenerator object 
            // and getting instance 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr 
                = AlgorithmParameterGenerator 
                      .getInstance("DiffieHellman"); 
  
            // creating Provider object 
            Provider pd = sr.getProvider(); 
  
            // getting algorithm name 
            // by using getAlgorithm() mathod 
            String algo = sr.getAlgorithm(); 
  
            // creating AlgorithmParameterGenerator object 
            // and getting instance 
            // 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@63947c6b

示例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 AlgorithmParameterGenerator object 
            // and getting instance 
            // 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(); 
  
            // creating AlgorithmParameterGenerator object 
            // and getting instance 
            // By usng getInstance() method 
            AlgorithmParameterGenerator sr1 
                = AlgorithmParameterGenerator 
                      .getInstance("GFG", 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:
 no such algorithm: GFG for provider SUN

参考: https://docs.oracle.com/javase/9/docs/api/java/security/AlgorithmParameters.html



相关用法


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