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


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


getInstance(String algorithm)

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

用法:

public static KeyFactory
  getInstance(String algorithm)
  throws NoSuchAlgorithmException

参数:此方法将标准算法视为要为其实例创建此KeyFactory实例的参数。


返回值:此方法返回一个新的 key 工厂对象。

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

  • NoSuchAlgorithmException:如果没有提供者可用于支持特定算法的关键工厂spi应用程序。
  • NullPointerException :如果algorithm为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 KeyFactory 
            // and getting instance 
            // By usng getInstance() method 
            KeyFactory sr 
                = KeyFactory.getInstance("DSA"); 
  
            // getting the algoritm of KeyFactory object 
            String str = sr.getAlgorithm(); 
  
            // printing the status 
            System.out.println("algorithm : " + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
algorithm : DSA

示例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 KeyFactory 
            // and getting instance 
            // By usng getInstance() method 
            KeyFactory sr = KeyFactory.getInstance("GEEKS"); 
  
            // getting the algoritm of KeyFactory object 
            String str = sr.getAlgorithm(); 
  
            // printing the status 
            System.out.println("algorithm : " + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
Exception thrown :
 java.security.NoSuchAlgorithmException:
 GEEKS KeyFactory not available

getInstance(String algorithm, String provider)

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

用法:

public static KeyFactory
  getInstance(String algorithm, String provider)
  throws NoSuchAlgorithmException

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


  • algorithm:这是为获取实例而指定的算法的名称。
  • provider:这是要指定的提供者的名称

返回值:此方法返回一个新的 key 工厂对象。

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

  • NoSuchAlgorithmException:–如果没有提供者可用于支持特定算法的关键工厂spi应用程序。
  • IllegalArgumentException:–如果提供者为null。
  • 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 KeyFactory 
            // and getting instance 
            // By usng getInstance() method 
            KeyFactory sr 
                = KeyFactory.getInstance( 
                    "DSA", "SUN"); 
  
            // getting the algoritm of KeyFactory object 
            String str = sr.getAlgorithm(); 
  
            // printing the status 
            System.out.println("algorithm : " + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NoSuchProviderException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
algorithm : DSA

示例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 KeyFactory 
            // and getting instance 
            // By usng getInstance() method 
            KeyFactory sr 
                = KeyFactory.getInstance( 
                    "RSA", "SUN"); 
  
            // getting the algoritm of KeyFactory object 
            String str = sr.getAlgorithm(); 
  
            // printing the status 
            System.out.println("algorithm : " + str); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NoSuchProviderException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
Exception thrown :
 java.security.NoSuchAlgorithmException:
 no such algorithm: RSA for provider SUN

参考: https://docs.oracle.com/javase/9/docs/api/java/security/KeyFactory.html#getInstance-java.lang.String-



相关用法


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