【发布时间】:2016-02-05 03:53:22
【问题描述】:
我读过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,但没有帮助。
【问题讨论】:
标签: java elliptic-curve ecdsa
我读过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,但没有帮助。
【问题讨论】:
标签: java elliptic-curve ecdsa
看看下面的代码,它是c#但在java中是类似的。在此示例中,私钥由 base64 编码字符串给出,并且还返回了 base64 编码字符串。注释的 keyParameters 正在工作,所以如果你想拥有 key 和 curve,请使用这个。
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());
}
【讨论】: