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
参考:
- https://docs.oracle.com/javase/9/docs/api/java/security/AlgorithmParameterGenerator.html#getInstance-java.lang.String-
- https://docs.oracle.com/javase/9/docs/api/java/security/AlgorithmParameterGenerator.html#getInstance-java.lang.String-java.security.Provider-
相关用法
- Java AlgorithmParameterGenerator init()用法及代码示例
- Java AlgorithmParameterGenerator getAlgorithm()用法及代码示例
- Java AlgorithmParameterGenerator generateParameters()用法及代码示例
- Java AlgorithmParameterGenerator getProvider()用法及代码示例
- Java KeyPairGenerator getInstance()用法及代码示例
- Java Calendar getInstance()用法及代码示例
- Java Signature getInstance()用法及代码示例
- Java DecimalFormatSymbols getInstance()用法及代码示例
- Java Currency getInstance()用法及代码示例
- Java NumberFormat getInstance()用法及代码示例
- Java MessageDigest getInstance()用法及代码示例
- Java KeyFactory getInstance()用法及代码示例
- Java SecureRandom getInstance()用法及代码示例
- Java AlgorithmParameters getInstance()用法及代码示例
- Java DecimalFormatSymbols getInstance(Locale)用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 AlgorithmParameterGenerator getInstance() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。