gpt4 book ai didi

java - 字符串到公钥

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:00 24 4
gpt4 key购买 nike

那里。我正在 java 应用程序和 php 服务器之间实现 Diffie Hellman key 交换协议(protocol)。当我从 php 服务器获取 key 时遇到一些问题,该 key 是字符串格式,我想将其转换为 PublicKey 来执行协议(protocol)。代码如下:

public static PublicKey getPublicKey(String publicKey) throws Exception {
byte[] byteKey =publicKey.getBytes();
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(byteKey);
KeyFactory keyFact = KeyFactory.getInstance("DH");
PublicKey pubKey = keyFact.generatePublic(x509KeySpec);
DHPublicKey dhPublicKey;
return pubKey;
}

当我运行这个函数时,我遇到了这个异常:

Exception in thread "main" java.security.spec.InvalidKeySpecException: Inappropriate key specification
at com.sun.crypto.provider.DHKeyFactory.engineGeneratePublic(DHKeyFactory.java:87)
at java.security.KeyFactory.generatePublic(Unknown Source)
at main2.getPublicKey(main2.java:47)
at main2.main(main2.java:108)

key 是一个大整数类型,例如:“2730833911517276610828829348409530515930034154987099616280024338836450623403836102419905982575105923164305996 145622906355021487887788144551871008554848191"我该如何纠正这个问题?感谢您的宝贵时间

最佳答案

感谢用户3062946,我已经解决了该问题。代码如下:

public static PublicKey getPublicKey(BigInteger publicKey, BigInteger p, BigInteger g) throws Exception {
DHPublicKeySpec dhspec=new DHPublicKeySpec(publicKey,p,g);
KeyFactory keyFact = KeyFactory.getInstance("DH");
PublicKey pubKey = keyFact.generatePublic(dhspec);

return pubKey;
}

关于java - 字符串到公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27969830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com