📌  相关文章
📜  Java中的 KeyPairGenerator initialize() 方法及示例

📅  最后修改于: 2022-05-13 01:55:24.321000             🧑  作者: Mango

Java中的 KeyPairGenerator initialize() 方法及示例

初始化(int keysize)

Java.security.KeyPairGeneratorinitialize()方法用于初始化KeyPairGenerator 对象以供进一步使用。

句法:

public void initialize(int keysize)

参数:此方法寻求 int 类型的keysize作为参数。
返回值:此方法没有任何返回值。
异常:如果传递大于或小于指定条件的值,则此方法抛出InvalidParameterException

注意:以下程序不会在在线 IDE 上运行。

下面是说明initialize(int keysize)方法的示例:

示例 1:

Java
// 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();
 
            // printing the Keypair
            System.out.println("Keypair : " + kp);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// 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();
 
            // printing 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);
        }
    }
}


Java
// 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();
 
            // printing 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);
        }
    }
}


Java
// 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();
 
            // printing 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@12a3a380

示例 2:对于InvalidParameterException

Java

// 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();
 
            // printing 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

初始化(int keysize,SecureRandom 随机)

Java.security.KeyPairGeneratorinitialize()方法使用 SecureRandom 对象为特定大小初始化 KeyPairGenerator 以供进一步使用。

句法:

public void initialize(int keysize,
                       SecureRandom random)

参数:此方法采用以下参数作为参数:

  • size : 这是密钥大小
  • random :这是 SecureRandom 类型的对象

返回值:该方法提供KeyPairGenerator的对象。
异常:如果传递大于或小于指定条件的值,则此方法抛出InvalidParameterException

下面是说明initialize()方法的示例:

示例 1:

Java

// 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();
 
            // printing 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

// 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();
 
            // printing 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-