1

I've read How do I obtain the public key from an ECDSA private key in OpenSSL?

and want to do the same thing, but in Java with Bouncy Castle.

I've also seen Bouncy Castle ESCDA Create Public Key from Private Key but it did not help.

1 Answer 1

1

take a look at the following code, it's c# but in java it is similar. the private key is given by as base64 encoded string in this example and also a base64 encoded string is given back. the commented keyParameters are working, so use this one if you want to have key and 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());
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.