如何将公共EC代码点和曲线名称转换为PublicKey? [英] How does one convert a public EC code point and curve name into a PublicKey?
问题描述
我有两个32字节长的字节数组,分别表示EC公共密钥的X和Y值。我知道该曲线为命名曲线 prime256v1。
I have two 32 byte long byte arrays representing the X and Y values for an EC Public Key. I know that the curve is the named curve "prime256v1".
如何将其转换为Java PublicKey对象?
How can I turn that into a Java PublicKey object?
JCE似乎不提供使用命名曲线的任何工具。
The JCE appears to provide no facilities whatsoever to use named curves.
Bouncycastle的示例代码似乎无法与任何版本的充气城堡一起编译找到。
Bouncycastle's example code does not appear to compile with any version of bouncycastle I can find.
WTF吗?
推荐答案
事实证明,实际上,这是另一种方法。显然,可以使用AlgorithmParameters类将具有命名曲线的ECGenParameterSpec转换为ECParameterSpec对象,可以将其与KeyFactory一起使用以生成PublicKey对象:
It turns out that there is, in fact, another way to do this. The AlgorithmParameters class can apparently be used to translate an ECGenParameterSpec, with a named curve, into an ECParameterSpec object that you can use with a KeyFactory to generate a PublicKey object:
ECPoint pubPoint = new ECPoint(new BigInteger(1, x),new BigInteger(1, y));
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC", "SunEC");
parameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPublicKeySpec pubSpec = new ECPublicKeySpec(pubPoint, ecParameters);
KeyFactory kf = KeyFactory.getInstance("EC");
return (ECPublicKey)kf.generatePublic(pubSpec);
这篇关于如何将公共EC代码点和曲线名称转换为PublicKey?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!