getInstance(String algorithm)
java.security.KeyPairGenerator類的getInstance()方法用於返回KeyPairGenerator對象,該對象為指定算法生成公用/專用 key 對。
此方法從最喜歡的提供者開始遍曆已注冊的安全提供者列表。返回一個新的KeyPairGenerator對象,該對象封裝了支持指定算法的第一個Provider的KeyPairGeneratorSpi實現。
用法:
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
參數:此方法將“算法”的標準名稱作為參數。
返回值:此方法返回新的KeyPairGenerator對象。
異常:如果沒有提供者支持指定算法的Signature實現,則此方法將引發NoSuchAlgorithmException。
下麵是說明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
// KeyPairGenerator
// and getting instance
// using getInstance() method
KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA");
// getting the Algorithm
String algo = sr.getAlgorithm();
// printing the algo String
System.out.println("Algorithm : " + algo);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (ProviderException 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
// KeyPairGenerator
// and getting instance
// using getInstance() method
System.out.println("Trying to get"
+ " the instance of unknown Algorithm");
KeyPairGenerator sr = KeyPairGenerator
.getInstance("TAJMAHAL");
// getting the Algorithm
String algo = sr.getAlgorithm();
// printing the algo String
System.out.println("Algorithm : " + algo);
}
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 Algorithm Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL KeyPairGenerator not available
getInstance(String algorithm, Provider provider)
java.security.KeyPairGenerator類的getInstance()方法用於返回KeyPairGenerator對象,該對象為指定算法生成公用/專用 key 對。
返回一個新的KeyPairGenerator對象,該對象封裝了來自指定Provider對象的KeyPairGeneratorSpi實現。請注意,指定的提供程序對象不必在提供程序列表中注冊。
用法:
public static KeyPairGenerator getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
參數:此方法將以下參數作為參數:
- algorithm:請求的算法的名稱。
- provider:提供者
返回值:此方法返回新的KeyPairGenerator對象。
異常:此方法引發以下異常:
- NoSuchAlgorithmException –如果指定的Provider對象不提供指定算法的SignatureSpi實現。
- IllegalArgumentException –如果提供者為null。
下麵是說明getInstance()方法的示例:
注意:以下程序將無法在在線IDE上運行。
示例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
// KeyPairGenerator
// and getting instance
// using getInstance() method
KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA");
// creating Provider object
Provider pd = sr.getProvider();
// getting algorithm name
// by using getAlgorithm() mathod
String algo = sr.getAlgorithm();
// creating the object of KeyPairGenerator and getting instance
// By usng getInstance() method
KeyPairGenerator sr1 = KeyPairGenerator.getInstance(algo, pd);
// getting the status of signature object
KeyPair keypair = sr1.generateKeyPair();
// printing the keypair
System.out.println("Keypair : " + keypair);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Keypair : java.security.KeyPair@12a3a380
示例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
// KeyPairGenerator
// and getting instance
// using getInstance() method
KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA");
// creating Provider object
Provider pd = sr.getProvider();
// creating the object of KeyPairGenerator and getting instance
// By usng getInstance() method
KeyPairGenerator sr1 = KeyPairGenerator.getInstance("TAJMAHAL", pd);
// getting the status of signature object
KeyPair keypair = sr1.generateKeyPair();
// printing the keypair
System.out.println("Keypair : " + keypair);
}
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 SUN
示例3:顯示IllegalArgumentException
// 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
// KeyPairGenerator
// and getting instance
// using getInstance() method
KeyPairGenerator sr = KeyPairGenerator
.getInstance("RSA");
// creating Provider object
System.out.println("Trying to assign null as a provider");
Provider pd = null;
// getting algorithm name
// by using getAlgorithm() mathod
String algo = sr.getAlgorithm();
// creating the object of KeyPairGenerator and getting instance
// By usng getInstance() method
KeyPairGenerator sr1 = KeyPairGenerator
.getInstance(algo, pd);
// getting the status of signature object
KeyPair keypair = sr1.generateKeyPair();
// printing the keypair
System.out.println("Keypair : " + keypair);
}
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);
}
}
}
Trying to assign null as a provider Exception thrown : java.lang.IllegalArgumentException: missing provider
相關用法
- Java KeyPairGenerator getAlgorithm()用法及代碼示例
- Java KeyPairGenerator getProvider()用法及代碼示例
- Java KeyPairGenerator initialize()用法及代碼示例
- Java KeyPairGenerator genKeyPair()用法及代碼示例
- Java KeyPairGenerator generateKeyPair()用法及代碼示例
- Java KeyFactory getInstance()用法及代碼示例
- Java DecimalFormatSymbols getInstance()用法及代碼示例
- Java NumberFormat getInstance()用法及代碼示例
- Java MessageDigest getInstance()用法及代碼示例
- Java Calendar getInstance()用法及代碼示例
- Java AlgorithmParameterGenerator getInstance()用法及代碼示例
- Java Signature getInstance()用法及代碼示例
- Java AlgorithmParameters getInstance()用法及代碼示例
- Java SecureRandom getInstance()用法及代碼示例
- Java Currency getInstance()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 KeyPairGenerator getInstance() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。