本文介绍了需要从 y、p、g 作为 BigIntegers 创建 DHPublicKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个 DHPublicKey 来加密一些数据.因此,我已经获得了所有需要的参数作为 BigIntegers (y, p, g).不幸的是,我没有看到从这些适合 DHPublicKey 接口的参数创建公钥对象的直接方法.有什么想法吗?
I need a DHPublicKey to encrypt some data.Therefore I have been provided with all the needed parameters as BigIntegers (y, p, g).Unfortunately I don't see a straight way for creating a public key object from these parameters that would fit the DHPublicKey interface.Any idea?
推荐答案
KeyFactory keyFactory;
KeyPairGenerator kpg;
DHPublicKey originalDhPubKey, fromSpecsDhPubKey;
DHPublicKeySpec dhPubKeySpecs;
KeyPair kp;
BigInteger p, g, y;
// generate a DH key pair
kpg = KeyPairGenerator.getInstance("DH");
kp = kpg.generateKeyPair();
// get the DH public key
originalDhPubKey = (DHPublicKey) kp.getPublic();
// get P, G and Y specs
p = originalDhPubKey.getParams().getP();
g = originalDhPubKey.getParams().getG();
y = originalDhPubKey.getY();
// get a DH KeyFactory
keyFactory = KeyFactory.getInstance("DH");
// create a DHPublicKeySpec with the specs you have
dhPubKeySpecs = new DHPublicKeySpec(y, p, g);
// get the DHPublicKey
fromSpecsDhPubKey = (DHPublicKey) keyFactory.generatePublic(dhPubKeySpecs);
// Check that the DH public values are equal
System.out.println(originalDhPubKey.getY().equals(fromSpecsDhPubKey.getY()));
这篇关于需要从 y、p、g 作为 BigIntegers 创建 DHPublicKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!