📌  相关文章
📜  带有示例的Java签名 getInstance() 方法

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

带有示例的Java签名 getInstance() 方法

getInstance(字符串算法)

Java.security.Provider 类的getInstance()方法用于返回实现指定签名算法的Signature 对象。

此方法遍历已注册的安全提供者列表,从最喜欢的提供者开始。返回一个新的 Signature 对象,该对象封装了来自支持指定算法的第一个 Provider 的 SignatureSpi 实现。

句法:

public static Signature getInstance(String algorithm)
    throws NoSuchAlgorithmException

参数:该方法以算法的标准名称为参数。
返回值:此方法返回新的 Signature 对象。
异常:如果没有提供者支持指定算法的签名实现,则此方法抛出NoSuchAlgorithmException

下面是说明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 Signature and getting instance
            // By using getInstance() method
            Signature sr = Signature.getInstance("SHA1WithRSA");
 
            // getting the status of signature object
            String str = sr.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException 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 Signature and getting instance
            // By using getInstance() method
            System.out.println("Trying to get the instance of unknown instance");
            Signature sr = Signature.getInstance("TAJMAHAL");
 
            // getting the status of signature object
            String str = sr.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException 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 Signature and getting instance
            // By using getInstance() method
            Signature sr = Signature.getInstance("SHA1WithRSA");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using     getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // creating the object of Signature and getting instance
            // By using getInstance() method
            Signature sr1 = Signature.getInstance(algo, pd);
 
            // getting the status of signature object
            String str = sr1.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException 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 Signature and getting instance
            // By using getInstance() method
            Signature sr = Signature.getInstance("SHA1WithRSA");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using     getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // creating the object of Signature and getting instance
            // By using getInstance() method
            Signature sr1 = Signature.getInstance("TAJMAHAL", pd);
 
            // getting the status of signature object
            String str = sr1.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException 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 Signature and getting instance
            // By using getInstance() method
            Signature sr = Signature.getInstance("SHA1WithRSA");
 
            // creating Provider object
            Provider pd = null;
 
            // getting algorithm name
            // by using     getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // creating the object of Signature and getting instance
            // By using getInstance() method
            Signature sr1 = Signature.getInstance(algo, pd);
 
            // getting the status of signature object
            String str = sr1.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
Status : Signature object: SHA1WithRSA

示例 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 Signature and getting instance
            // By using getInstance() method
            System.out.println("Trying to get the instance of unknown instance");
            Signature sr = Signature.getInstance("TAJMAHAL");
 
            // getting the status of signature object
            String str = sr.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Trying to get the instance of unknown instance
Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL Signature not available

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

Java.security.Provider 类的getInstance()方法用于返回实现指定签名算法的Signature 对象。

返回一个封装来自指定 Provider 对象的 SignatureSpi 实现的新 Signature 对象。请注意,指定的提供者对象不必在提供者列表中注册。

句法:

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

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

  • algorithm – 请求的算法的名称。
  • 提供者——提供者

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

异常:此方法抛出以下异常:

  • NoSuchAlgorithmException – 如果指定算法的 SignatureSpi 实现在指定的 Provider 对象中不可用。
  • IllegalArgumentException – 如果提供者为空。

下面是说明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 Signature and getting instance
            // By using getInstance() method
            Signature sr = Signature.getInstance("SHA1WithRSA");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using     getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // creating the object of Signature and getting instance
            // By using getInstance() method
            Signature sr1 = Signature.getInstance(algo, pd);
 
            // getting the status of signature object
            String str = sr1.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Status : Signature object: SHA1WithRSA

示例 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 Signature and getting instance
            // By using getInstance() method
            Signature sr = Signature.getInstance("SHA1WithRSA");
 
            // creating Provider object
            Provider pd = sr.getProvider();
 
            // getting algorithm name
            // by using     getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // creating the object of Signature and getting instance
            // By using getInstance() method
            Signature sr1 = Signature.getInstance("TAJMAHAL", pd);
 
            // getting the status of signature object
            String str = sr1.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: TAJMAHAL for provider SunRsaSign

示例 3:显示 IllegalArgumentException

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 Signature and getting instance
            // By using getInstance() method
            Signature sr = Signature.getInstance("SHA1WithRSA");
 
            // creating Provider object
            Provider pd = null;
 
            // getting algorithm name
            // by using     getAlgorithm() method
            String algo = sr.getAlgorithm();
 
            // creating the object of Signature and getting instance
            // By using getInstance() method
            Signature sr1 = Signature.getInstance(algo, pd);
 
            // getting the status of signature object
            String str = sr1.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Exception thrown : java.lang.IllegalArgumentException: missing provider