我看过How do I obtain the public key from an ECDSA private key in OpenSSL?
并希望做同样的事情,但在Java中使用Bouncy Castle。
我也看过Bouncy Castle ESCDA Create Public Key from Private Key,但没有帮助。
答案 0 :(得分:0)
看看下面的代码,它是c#,但在java中它是相似的。私钥在此示例中由as64编码的字符串给出,并且还返回base64编码的字符串。注释的keyParameters正在工作,所以如果你想拥有键和曲线,请使用这个。
private static readonly Org.BouncyCastle.Asn1.X9.X9ECParameters curve = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256r1");
private static readonly Org.BouncyCastle.Crypto.Parameters.ECDomainParameters domain = new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
public string GetPublicKey(string privKey)
{
Org.BouncyCastle.Math.BigInteger d = new Org.BouncyCastle.Math.BigInteger(Convert.FromBase64String(privKey));
//var privKeyParameters = new Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters(d, domain);
Org.BouncyCastle.Math.EC.ECPoint q = domain.G.Multiply(d);
//var pubKeyParameters = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);
return Convert.ToBase64String(q.GetEncoded());
}