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


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


java.security.KeyFactory类的generatePublic()方法用于根据提供的 key 规范( key 材料)生成公共 key 对象。

用法:

public final PublicKey generatePublic(KeySpec keySpec)
                               throws InvalidKeySpecException

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


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

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

以下示例说明了generatePrivate()方法

注意:以下程序将无法在在线IDE上运行

范例1:

// Java program to demonstrate 
// generatePublic()  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 
            PublicKey prv = kp.getPublic(); 
  
            // getting byte data of Public key 
            byte[] publicKeyBytes = prv.getEncoded(); 
  
            // creating keyspec object 
            EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes); 
  
            // creating object of keyfactory 
            KeyFactory keyFactory = KeyFactory.getInstance("DSA"); 
  
            // generating Public key from the provided key spec. 
            // using generatePublic() method 
            PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); 
  
            // printing public key 
            System.out.println("public key:" + publicKey); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
输出:
publickey:Sun DSA Public Key
    Parameters:DSA
        p:    fd7f5381 1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
    455d4022 51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
    6b9950a5 a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
    83f6d3c5 1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
        q:    9760508f 15230bcc b292b982 a2eb840b f0581cf5
        g:    f7e1a085 d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
    5159578e bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
    3c167a8b 547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
    cca4f1be a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a

  y:
    8f046035 5f22ccd0 56bd5d71 1f0cf770 8a420f82 4cc833ca b9585b39 6a84d4c6
    516ad382 420ec304 e69fc277 36d87647 c00e4de4 a86d9e2f abc7d75f 35bb0246
    b8a20e96 84af9676 fc9f5d2f 114b48ca 4179d9e1 81c1cbd7 d4a16948 03861f10
    64860410 345606af 5df74878 469d7661 ac486f41 58548100 fa3c6705 5be34d24

范例2:对于InvalidKeySpecException

// Java program to demonstrate 
// generatePublic()  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 
            PublicKey prv = kp.getPublic(); 
  
            // getting byte data of Public key 
            byte[] publicKeyBytes = prv.getEncoded(); 
  
            // creating keyspec object 
            EncodedKeySpec publicKeySpec = new PKCS8EncodedKeySpec(publicKeyBytes); 
  
            // creating object of keyfactory 
            KeyFactory keyFactory = KeyFactory.getInstance("DSA"); 
  
            // generating Public key from the provided key spec. 
            // using generatePublic() method 
            PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); 
  
            // printing public key 
            System.out.println("public key:" + publicKey); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
        catch (ProviderException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
输出:
Exception thrown:java.security.spec.InvalidKeySpecException:
Inappropriate key specification


相关用法


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