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

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

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

getInstance(字符串算法)

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

public static AlgorithmParameterGenerator 
  getInstance(String algorithm)
    throws NoSuchAlgorithmException

参数:该方法以算法的标准名称为参数。
返回值:此方法返回新的 AlgorithmParameterGenerator 对象。
异常:此方法抛出以下异常:

  • NoSuchAlgorithmException:如果没有 Provider 支持指定算法的 AlgorithmParameterGeneratorSpi 实现。
  • NullPointerException:如果指定的算法为空。

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

Java
// 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 using 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);
        }
    }
}


Java
// 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 using 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);
        }
    }
}


Java
// 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 using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("DSA");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using 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);
        }
    }
}


Java
// 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 using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("GFG");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using 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

// 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 using 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

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

Java.security.AlgorithmParameterGenerator 类getInstance()方法用于返回一个AlgorithmParameterGenerator 对象,该对象实现了指定的签名算法和指定的provider 对象。
句法:

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

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

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

返回值:此方法返回新的 AlgorithmParameterGenerator 对象。
异常:此方法抛出以下异常:

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

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

Java

// 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 using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("DSA");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using 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

// 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 using getInstance() method
            AlgorithmParameterGenerator sr
                = AlgorithmParameterGenerator
                      .getInstance("GFG");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // Getting instance of
            // AlgorithmParameterGenerator
            // By using 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-