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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。