📜  Java中的 KeyFactory generatePrivate() 方法及示例

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

Java中的 KeyFactory generatePrivate() 方法及示例

Java.security.KeyPairGenerator类的generatePrivate()方法用于从提供的密钥规范(密钥材料)生成私钥对象。

句法:

public final PrivateKey generatePrivate(KeySpec keySpec)
                                 throws InvalidKeySpecException

参数:该方法以keySpec(私钥的规范(密钥材料))为参数。

返回值:此方法返回私钥。

异常:如果给定的密钥规范不适合此密钥工厂生成私钥,则此方法将引发InvalidKeySpecException

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

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

示例 1:

// Java program to demonstrate
// generatePrivate() method
  
import java.security.*;
import java.util.*;
import java.security.spec.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating the object of KeyPairGenerator
            KeyPairGenerator kpg = KeyPairGenerator
                                       .getInstance("DSA");
  
            // initializing with 1024
            kpg.initialize(1024);
  
            // getting key pairs
            // using generateKeyPair() method
            KeyPair kp = kpg.genKeyPair();
  
            // getting public key
            PrivateKey prv = kp.getPrivate();
  
            // getting byte data of private key
            byte[] private KeyBytes = prv.getEncoded();
  
            // creating keyspec object
            EncodedKeySpec
                private KeySpec
                = new PKCS8EncodedKeySpec(private KeyBytes);
  
            // creating object of keyfactory
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
  
            // generating private key from the provided key spec.
            // using generatePrivate() method
            PrivateKey private Key = keyFactory
                                        .generatePrivate(private KeySpec);
  
            // printing private key
            System.out.println("PrivateKey : " + private Key);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
PrivateKey : sun.security.provider.DSAPrivateKey@fff96ed9

示例 2:对于InvalidKeySpecException

// Java program to demonstrate
// generatePrivate() method
  
import java.security.*;
import java.util.*;
import java.security.spec.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating the object of KeyPairGenerator
            KeyPairGenerator kpg = KeyPairGenerator
                                       .getInstance("DSA");
  
            // initializing with 1024
            kpg.initialize(1024);
  
            // getting key pairs
            // using generateKeyPair() method
            KeyPair kp = kpg.genKeyPair();
  
            // getting public key
            PrivateKey prv = kp.getPrivate();
  
            // getting byte data of private key
            byte[] private KeyBytes = prv.getEncoded();
  
            // creating keyspec object
            EncodedKeySpec
                private KeySpec
                = new X509EncodedKeySpec(private KeyBytes);
  
            // creating object of keyfactory
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
  
            // generating private key from the provided key spec.
            // using generatePrivate() method
            PrivateKey private Key = keyFactory
                                        .generatePrivate(private KeySpec);
  
            // printing private key
            System.out.println("Private Key : " + private Key);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (InvalidKeySpecException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Exception thrown : java.security.spec.InvalidKeySpecException:
 Inappropriate key specification