2

I have the x and y coordinate of a point and the name of a curve. I now want to create an org.bouncycastle.jce.interfaces.ECPublicKey object from that, automatically using the implementation that is provided. The goal is to be able to create the objects no matter if bouncycastle or its Android port, spongycastle, is used.

This is what I'm doing right now. Thing is, the EC5Util class is not included in spongycastle. I'd like to have a solution using maybe a factory with just one method I have to call. Is that possible?

java.security.spec.ECPoint w = new java.security.spec.ECPoint(x, y);
ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");
ECCurve curve = params.getCurve();
java.security.spec.EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, params.getSeed());
java.security.spec.ECParameterSpec params2 = EC5Util.convertSpec(ellipticCurve, params);
java.security.spec.ECPublicKeySpec keySpec = new java.security.spec.ECPublicKeySpec(w, params2);
return (ECPublicKey) fact.generatePublic(keySpec);
Sibbo
  • 3,796
  • 2
  • 23
  • 41
  • Different approaches, but nothing that is simple and just works. I have to go a huge way around of what I'm actually doing. I'll paste the code in. – Sibbo Sep 20 '13 at 23:54

2 Answers2

2

The unit tests within Bouncycastle, in ECPointTest.java, appears to have coverage for this case:

ECFieldElement.Fp x_ecfe = new ECFieldElement.Fp(q, x);
ECFieldElement.Fp y_ecfe = new ECFieldElement.Fp(q, y);
ECPoint.Fp point = new ECPoint.Fp(curve, x_ecfe, y_ecfe);

This is a very unique situation you are in. If you cared to expand why you are doing this I'd be interested to find out.

  • Well, I used this way some time ago, but I think the classes you are using are not part of spongycastle. I'm writing an application in Java (JBitmessage), and some guy wants to create an Android port of that. For Android, he has to use spongycastle, which seems to use different classes for the keys. If I now create the ECPoint.Fp objects it will create an error when you use spongycastle, since those classes don't exist. I thought of a factory that exists somewhere in the bouncycastle code that can be used. – Sibbo Sep 20 '13 at 23:50
  • I see. In that case, if I were in your shoes I'd just try encoding the key as an uncompressed point pair, then use a key factory to load it. That's just me though. I do wonder why you don't have an encoded key to start out with though. – Benjamin Damm Sep 23 '13 at 22:04
2

So I know its been a while since this was posted. but turns out today it is easier to do at least when you have an instance o the Curve object.

ECCurve curve = .....
BigInteger x = .....
BigInteger y = .....

ECPoint.Fp point =  curve.createPoint(x,y);

Hope it helps.

jstuartmilne
  • 4,398
  • 1
  • 20
  • 30