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


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


initialize(int keysize)

可以使用java.security.KeyPairGenerator的initialize()方法來初始化KeyPairGenerator對象,以供進一步使用。

用法:

public void initialize(int keysize)

參數:此方法將int類型的keysize用作參數。


返回值:此方法無返回值。

異常:如果傳遞的值大於或小於指定的條件,則此方法將引發InvalidParameterException。

注意:以下程序將無法在在線IDE上運行。

下麵的示例說明了initialize(int keysize)方法:

示例1:

// Java program to demonstrate 
// genKeyPair() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // creating the object of KeyPairGenerator 
            KeyPairGenerator kpg 
                = KeyPairGenerator 
                      .getInstance("RSA"); 
  
            // initializing with 1024 
            kpg.initialize(1024); 
  
            // getting key pairs 
            // using genKeyPair() method 
            KeyPair kp = kpg.genKeyPair(); 
  
            // pritning the Keypair 
            System.out.println("Keypair : " + kp); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Keypair : java.security.KeyPair@12a3a380

示例2:對於InvalidParameterException

// Java program to demonstrate 
// genKeyPair() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // creating the object of KeyPairGenerator 
            KeyPairGenerator kpg 
                = KeyPairGenerator 
                      .getInstance("RSA"); 
  
            // initializing with 1024 
            kpg.initialize(-24); 
  
            // getting key pairs 
            // using genKeyPair() method 
            KeyPair kp = kpg.genKeyPair(); 
  
            // pritning the Keypair 
            System.out.println("Keypair : " + kp); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (InvalidParameterException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Exception thrown :
 java.security.InvalidParameterException:
 RSA keys must be at least 512 bits long

initialize(int keysize, SecureRandom random)

java.security.KeyPairGenerator的initialize()方法使用SecureRandom對象針對特定大小初始化KeyPairGenerator,以供進一步使用。

用法:


public void initialize(int keysize,
                       SecureRandom random)

參數:此方法將以下參數作為參數:

  • size:這是鍵大小
  • random:這是SecureRandom類型的對象

返回值:此方法提供KeyPairGenerator的對象。

異常:如果傳遞的值大於或小於指定的條件,則此方法將引發InvalidParameterException。

下麵是說明initialize()方法的示例:

示例1:

// Java program to demonstrate 
// genKeyPair() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] argv) 
        throws Exception 
    { 
        try { 
  
            // creating the object of KeyPairGenerator 
            KeyPairGenerator kpg 
                = KeyPairGenerator 
                      .getInstance("RSA"); 
  
            // creating the object of SecureRandom 
            SecureRandom se 
                = SecureRandom.getInstance("SHA1PRNG"); 
  
            // initializing with 1024 
            kpg.initialize(1024, se); 
  
            // getting key pairs 
            // using genKeyPair() method 
            KeyPair kp = kpg.genKeyPair(); 
  
            // pritning the Keypair 
            System.out.println("Keypair : " + kp); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (InvalidParameterException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Keypair : java.security.KeyPair@4e25154f

示例2:對於InvalidParameterException

// Java program to demonstrate 
// genKeyPair() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] argv) 
        throws Exception 
    { 
        try { 
  
            // creating the object of KeyPairGenerator 
            KeyPairGenerator kpg 
                = KeyPairGenerator 
                      .getInstance("RSA"); 
  
            // creating the object of SecureRandom 
            SecureRandom se 
                = SecureRandom.getInstance("SHA1PRNG"); 
  
            // initializing with -24 
            kpg.initialize(-24, se); 
  
            // getting key pairs 
            // using genKeyPair() method 
            KeyPair kp = kpg.genKeyPair(); 
  
            // pritning the Keypair 
            System.out.println("Keypair : " + kp); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (InvalidParameterException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Exception thrown :
 java.security.InvalidParameterException:
 RSA keys must be at least 512 bits long

參考: https://docs.oracle.com/javase/9/docs/api/java/security/KeyPairGenerator.html#initialize-int-java.security.SecureRandom-



相關用法


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