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
相關用法
- Java KeyFactory getProvider()用法及代碼示例
- Java KeyFactory generatePublic()用法及代碼示例
- Java KeyFactory getAlgorithm()用法及代碼示例
- Java KeyFactory getInstance()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 KeyFactory generatePrivate() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。