如何做Diffie Hellman密钥生成和检索Java中的原始密钥字节 [英] How to do Diffie Hellman Key Generation and retrieve raw key bytes in Java

查看:221
本文介绍了如何做Diffie Hellman密钥生成和检索Java中的原始密钥字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中为现有程序编写测试工具。作为这一部分,我需要生成一个Diffie Hellman密钥对,并将公钥传递给其原始(即未编码的字节)形式的其他程序。



密钥对使用以下代码:

  KeyPairGenerator kpg = KeyPairGenerator.getInstance(DiffieHellman); 
kpg.initialize(512);
KeyPair dkp = kpg.generateKeyPair();但是,我似乎无法检索键的原始字节值:-(调用<$ c <$ c <$> dkp.getPublic()。getEncoded()
返回一个字节数组,但它的key是x509编码格式。



可能的前进方式发生在我身上:


  1. 找到一些以原始形式获取上述关键数据的方法。
  2. 将密钥的x509编码解码为原始形式

  3. 以不同的方式生成密钥,允许访问原始密钥

但是我不知道如何去做任何一个(这将是最好的)?



任何帮助或建议都将非常感谢。

解决方案

你可以得到X和Y c> Y = G ^ X mod P )值如下:

  BigInteger x = javax.crypto.interfaces.DHPrivateKey)dkp.getPrivate())。getX(); 
BigInteger y =((javax.crypto.interfaces.DHPublicKey)dkp.getPublic())。getY();

您可以从公钥或私钥获取G和P值,如下所示:

  DHParameterSpec params = 
((javax.crypto.interfaces.DHPublicKey)dkp.getPublic())getParams
BigInteger p = params.getP();
BigInteger g = params.getG();

从这里你可以得到它们作为原始字节数组:

  byte [] xBytes = x.toByteArray(); 
byte [] yBytes = y.toByteArray();
byte [] pBytes = p.toByteArray();
byte [] gBytes = g.toByteArray();

Y,P和G的组合构成公钥。 X应该保密。


I am writing a test harness in java for an existing program. As part of this i need to generate a Diffie Hellman key pair and pass the public key to the other program in its raw (i.e unencoded bytes) form.

I can successfully the key pair using the following code:

KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");
kpg.initialize(512);
KeyPair dkp = kpg.generateKeyPair();

However, i cannot seem to retrieve the raw byte value of the keys :-( Calling dkp.getPublic().getEncoded() returns a byte array but its of the Key in an x509 encoded format.

Three possible ways forward occur to me:

  1. Find some method of getting the key data out of the above in its raw form.
  2. Decode the x509 encoding of the key into its raw form
  3. Generate the keys in a different manner that allows access to the raw key

But im not how to go about doing any of them (and which will turn out to be best)?

Any help or advice would be greatly appreciated!

解决方案

You can get the X and Y (where Y = G^X mod P) values like this:

 BigInteger x = ((javax.crypto.interfaces.DHPrivateKey) dkp.getPrivate()).getX();
 BigInteger y = ((javax.crypto.interfaces.DHPublicKey) dkp.getPublic()).getY();

You can get the G and P values from either the public or private key like this:

DHParameterSpec params = 
    ((javax.crypto.interfaces.DHPublicKey) dkp.getPublic()).getParams();
BigInteger p = params.getP();
BigInteger g = params.getG();

From there you can get them all as raw byte arrays:

 byte[] xBytes = x.toByteArray();
 byte[] yBytes = y.toByteArray();
 byte[] pBytes = p.toByteArray();
 byte[] gBytes = g.toByteArray();

The combination of Y, P, and G make the public key. X should be kept secret.

这篇关于如何做Diffie Hellman密钥生成和检索Java中的原始密钥字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆