为什么在每次执行时Diffie Hellman公共参数都相同?
作者:互联网
我正在尝试实现Diffie Hellman密钥交换协议.目前,我还停留在公共参数生成上.
每次我运行程序时,p和g参数都是相同的(尽管方法文档说“每次调用都会生成一个新的密钥对.”).
请问有人向我解释我在这里想念什么吗?
KeyPairGenerator kpg;
try
{
kpg = KeyPairGenerator.getInstance("DiffieHellman");
kpg.initialize(512, new SecureRandom());
KeyPair dkp = kpg.generateKeyPair();
DHParameterSpec params =
((javax.crypto.interfaces.DHPublicKey) dkp.getPublic()).getParams();
BigInteger p = params.getP();
BigInteger a = params.getG();
System.out.println(p);
} catch (Exception e)
{
e.printStackTrace();
}
解决方法:
您没有为Diffie-Hellman交换显式初始化p和g值,因此它们已被初始化为默认值.请注意,这些值是公共的,并且必须在双方之间共享,以便交换正常工作.我遇到了一个死胡同after the third SPI in the Sun JCE,但是由于您自己没有设置参数,因此代码正在检索与DSA相同的默认p和g并将它们应用于DH.
In case the client does not explicitly initialize the AlgorithmParameterGenerator (via a call to an init method), each provider must supply (and document) a default initialization. For example, the Sun provider uses a default modulus prime size of 1024 bits for the generation of DSA parameters.
The documentation for the Sun implementation列出了512位密钥的以下值:
p = fca682ce 8e12caba 26efccf7 110e526d b078b05e decbcd1e b4a208f3
ae1617ae 01f35b91 a47e6df6 3413c5e1 2ed0899b cd132acd 50d99151
bdc43ee7 37592e17
g = 678471b2 7a9cf44e e91a49c5 147db1a9 aaf244f0 5a434d64 86931d2d
14271b9e 35030b71 fd73da17 9069b32e 2935630e 1c206235 4d0da20a
6c416e50 be794ca4
标签:cryptography,diffie-hellman,java 来源: https://codeday.me/bug/20191122/2058423.html