我想知道这是否是通过以下网址的HEX字符串在Java中创建PrivateKey对象的正确方法:
从十六进制字符串创建BigInteger:
BigInteger priv = new BigInteger(privateKeyFromSite, 16);
并传递给此方法:
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.jce.spec.ECPrivateKeySpec;
public static PrivateKey getPrivateKeyFromECBigIntAndCurve(BigInteger s, String curveName) {
ECParameterSpec ecParameterSpec = ECNamedCurveTable.getParameterSpec(curveName);
ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, ecParameterSpec);
try {
KeyFactory keyFactory = KeyFactory.getInstance(EC);
return keyFactory.generatePrivate(privateKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
return null;
}
}
答案 0 :(得分:3)
是的,EC私钥只是一个数字。如果您打印出PrivateKey
,则会看到相应公钥的X和Y坐标。
例如,假设生成了以下密钥对(secp256r1
):
EC私钥:
1b9cdf53588f99cea61c6482c4549b0316bafde19f76851940d71babaec5e569
EC公钥:
0458ff2cd70c9a0897eb90a7c43d6a656bd76bb8089d52c259db6d9a45bfb37eb9882521c3b1e20a8bae181233b939174ee95e12a47bf62f41a62f1a20381a6f03
我们将私钥字节插入您的函数中
BigInteger priv = new BigInteger("1b9cdf53588f99cea61c6482c4549b0316bafde19f76851940d71babaec5e569", 16);
PrivateKey privateKey = getPrivateKeyFromECBigIntAndCurve(priv, "secp256r1");
System.out.println(privateKey);
并打印:
EC Private Key [91:05:8a:28:94:f9:5c:cb:c4:34:b8:69:e4:39:d4:57:59:c7:51:35]
X: 58ff2cd70c9a0897eb90a7c43d6a656bd76bb8089d52c259db6d9a45bfb37eb9
Y: 882521c3b1e20a8bae181233b939174ee95e12a47bf62f41a62f1a20381a6f03
如您所见,如果连接04
+ X + Y,您将得到原始的公钥,(04
是未压缩的EC点标记)