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

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

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

getInstance(字符串算法)

Java.security.AlgorithmParameters类的getInstance()方法返回一个 AlgorithmParameters 类型的对象,该对象应用分配的 AlgorithmParameters 算法。
句法:

public static AlgorithmParameters
  getInstance(String algorithm)
  throws NoSuchAlgorithmException

参数:此方法寻求标准算法作为参数。
返回值:该方法提供了一个新的算法参数对象。
异常:此方法抛出以下异常:

  • NoSuchAlgorithmException:–如果没有提供程序可用于支持特定算法的算法参数 spi 应用程序。
  • 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 {
            // creating the object of AlgorithmParameters
            // and getting instance
            // By using 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);
        }
    }
}


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


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


输出:
Status : null

示例 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 {
 
            // creating the object of AlgorithmParameters
            // and getting instance
            // By using 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(字符串算法,Provider提供者)

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

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

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

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

返回值:该方法提供了一个新的算法参数对象。
异常:此方法抛出以下异常:

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

示例 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 {
            // creating AlgorithmParameterGenerator object
            // and getting instance
            // 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();
 
            // creating AlgorithmParameterGenerator object
            // and getting instance
            // By using 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