當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java KeyPairGenerator getInstance()用法及代碼示例


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
    


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 KeyPairGenerator getInstance() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。