使用Diffie-Hellman算法的String到PublicKey [英] String to PublicKey using Diffie-Hellman algorithm

查看:275
本文介绍了使用Diffie-Hellman算法的String到PublicKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公钥字符串(128字节到十六进制处理)由我的客户端给出。我需要使用私钥和公钥由客户端生成共享密钥。将字符串转换为公用密钥时,我得到下面的异常。我尝试解码/编码字节,没有改进。我有以下代码。



//这是一个示例键。

 私有静态最后字符串PUB_KEY = 0DC1B7102DE3F6785A284ABFCA1822A6B59C947B5F2FAAE + 672D8EE29C3D801BC153777CD3AF5478FD25C234C50BBABF8CD5215A8F1CB19B0B4A24FD5E9 + 412264646E2A06FCB5929FFBE196A1BD58B9927424C3B3D0388FDDA15FD1FF1C3E7600A629E + 
的B3F0B38B85CCCE03D44CF8D53B2E4E5EFD54E991CE92E55B10FCCD79F04;

public static void main(String [] argv)throws Exception {
PublicKey key = getKey(h2b(PUB_KEY));
}

private static PublicKey getKey(final byte [] pubKey)throws Exception {
final KeyFactory keyFactory = KeyFactory.getInstance(DH);
final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubKey);
return keyFactory.generatePublic(keySpec); // THROWS EXCEPTION
}

private static byte [] h2b(String hex){
if((hex.length()& 0x01)== 0x01)
throw new IllegalArgumentException();
byte [] bytes = new byte [hex.length()/ 2]; (int idx = 0; idx< bytes.length; ++ idx)
int hi = Character.digit((int)hex.charAt(idx * 2),16);
int lo = Character.digit((int)hex.charAt(idx * 2 + 1),16);
if((hi< 0)||(lo< 0))
throw new IllegalArgumentException();
bytes [idx] =(byte)((hi< 4)| lo);
}
返回字节;
}

抛出以下兴趣。任何帮助来解决这个问题?



线程中的异常mainjava.security.spec.InvalidKeySpecException:com.sun中不正确的密钥规范
。 crypto.provider.DHKeyFactory.engineGeneratePublic(DHKeyFactory.java:87)

解决方案

X509EncodedKeySpec 预计将包含与ASN.1 SubjectPublicKeyInfo 结构的字节数组。



您的示例中的 PUB_KEY 可能是原始密钥值( y javax.crypto.spec.DHPublicKeySpec ),这不足以创建keyspec。



您应该向您的客户请求正确的公众键入ASN.1表单作为 X509EncodedKeySpec (这是更可取的)的输入,或 p g DHPublicKeySpec 所需的参数。


I have a Public key string (128 bytes byte to hex processed) given by my client. I need to generate shared key using the private key and Public key given by client. I'm getting below exception while converting the String to Public key. I tried decoding/encoding the bytes, no improvement. I have the following code.

// This is a sample key.

private static final String PUB_KEY = "0DC1B7102DE3F6785A284ABFCA1822A6B59C947B5F2FAAE" + "672D8EE29C3D801BC153777CD3AF5478FD25C234C50BBABF8CD5215A8F1CB19B0B4A24FD5E9" + "412264646E2A06FCB5929FFBE196A1BD58B9927424C3B3D0388FDDA15FD1FF1C3E7600A629E" + 
"B3F0B38B85CCCE03D44CF8D53B2E4E5EFD54E991CE92E55B10FCCD79F04";

public static void main(String[] argv) throws Exception {   
    PublicKey key = getKey(h2b(PUB_KEY));
}

private static PublicKey getKey(final byte[] pubKey) throws Exception {
    final KeyFactory keyFactory = KeyFactory.getInstance("DH");  
    final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubKey);
    return keyFactory.generatePublic(keySpec); // THROWS EXCEPTION
}

private static byte[] h2b(String hex) {
    if ((hex.length() & 0x01) == 0x01)
        throw new IllegalArgumentException();
    byte[] bytes = new byte[hex.length() / 2];
    for (int idx = 0; idx < bytes.length; ++idx) {
        int hi = Character.digit((int) hex.charAt(idx * 2), 16);
        int lo = Character.digit((int) hex.charAt(idx * 2 + 1), 16);
        if ((hi < 0) || (lo < 0))
            throw new IllegalArgumentException();
        bytes[idx] = (byte) ((hi << 4) | lo);
    }
    return bytes;
}

Throws follwoing excetpion. Any help to solve this?

Exception in thread "main" java.security.spec.InvalidKeySpecException: Inappropriate key specification at com.sun.crypto.provider.DHKeyFactory.engineGeneratePublic(DHKeyFactory.java:87)

解决方案

The X509EncodedKeySpec is expected to contain an array of bytes with the ASN.1 SubjectPublicKeyInfo structure.

The PUB_KEY in your example is probably the raw key value (y in the terms of javax.crypto.spec.DHPublicKeySpec) which is not enough to create the keyspec.

You should ask your client eother for the proper public key in the ASN.1 form to be used as input for X509EncodedKeySpec (this is preferable) or for p and g parameters required for DHPublicKeySpec.

这篇关于使用Diffie-Hellman算法的String到PublicKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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