1

I'm developing an application that will store ECC public keys in a database. I used a existing code to generate the keys, but when I use the method to get the public key from a private key, I get two values ​​(x and y coordinates) returned. Is there a java-ready function that allows me to generate a public key from these two values? I found a link explaining how to do it, but I did not understand how to use the method.

package test;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.security.*;
import java.security.spec.*;
public class test {
    public static void main(String[] args) {
        KeyPairGenerator kpg;
        kpg = KeyPairGenerator.getInstance("EC","SunEC");
        ECGenParameterSpec ecsp;
        ecsp = new ECGenParameterSpec("secp256r1");
        kpg.initialize(ecsp);
        String key;
        KeyPair kp = kpg.genKeyPair();
        PrivateKey privKey = kp.getPrivate();
        PublicKey pubKey = kp.getPublic();
        key = pubKey.toString().substring(46,125).concat(pubKey.toString().substring(142,220));
}
}
Mutante
  • 278
  • 5
  • 18
  • What is your confusion? Elliptic Curve points have two coordinates. I'm calling your question as a dupe of [this](https://stackoverflow.com/a/30184609/1820553) – kelalaka Apr 16 '19 at 17:28
  • Yes, i've tried this solution but i couldnt generate one single public key using the x and y coordinates. – Mutante Apr 16 '19 at 17:41
  • 2
    This is Stackoverflow where is your code and errors? – kelalaka Apr 16 '19 at 17:42
  • I edited the post. In my code I capture x and y coordinates and the concatenates to be stored in my db. But I believe the concatenated x and y coordinates are not the public key. Looking for an explanation I found the post you said was duplicated, but I could not capture the coordinates in BigInteger and could not set the `ecParameters` parameter in `ECPublicKeySpec (ecPoint, ecParameters)`. Does this parameter match which value of my code? – Mutante Apr 16 '19 at 18:30
  • 1
    The [PublicKey](https://docs.oracle.com/javase/7/docs/api/java/security/PublicKey.html) is [serializable](https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html). Therefore, you can use this interface for [DB](https://javapapers.com/core-java/serialize-de-serialize-java-object-from-database/) – kelalaka Apr 16 '19 at 19:24
  • 5
    Your extraction from `(EC)PublicKey.toString()` is unsafe and will frequently produce an invalid result; it uses `BigInteger.toString()` which is decimal and varies in length. If you must store 'raw' x,y, which is not a good plan for flexibility in the future, use `.getW().getAffine{X,Y}()` to get the `BigInteger` values and then express them in a _recoverable_ form of your choice, such as fixed-length hex or delimited decimal. But much better use the standard Java encoding as kelalaka says or just the crypto-standard encoding `PublicKey.getEncoded()` reversed by `KeyFactory.generatePublic()` – dave_thompson_085 Apr 16 '19 at 19:36
  • 1
    Would [this answer](https://stackoverflow.com/a/26159150/589259) help you? – Maarten Bodewes Apr 16 '19 at 19:57
  • Thanks! I used `PublicKey.getEncoded()`. @MaartenBodewes your comment was useful too! – Mutante Apr 17 '19 at 17:00

0 Answers0