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


Java KeyFactory generatePrivate()用法及代码示例


java.security.KeyPairGenerator类的generatePrivate()方法用于根据提供的 key 规范( key 材料)生成私有 key 对象。

用法:

public final PrivateKey generatePrivate(KeySpec keySpec)
                                 throws InvalidKeySpecException

参数:此方法将keySpec(私钥的规范( key 材料))作为参数。


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

异常:如果给定的 key 规范不适合此 key 工厂产生私钥,则此方法将引发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


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 KeyFactory generatePrivate() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。