java - Converting byte array to publickey ECDSA -


i need use ecdsa algorithm sign message , send receiver in java. then, receiver should verify sender's signature.

so, this, receiver has sender's public key in byte array format after converting java.security.publickey byte array command bellow:

byte[] bytearraypublickey = publickey.getencoded(); 

the format of public key in ecdsa algorithm (before converting byte array) follow:

public key:

x: 8a83c389e7bb817c17bf2db4ed71055f18342b630221b2a3a1ca752502dc2e21  y: 3eaf48c9ab1700fe0966a0cde196b85af66bb8f0bacef711c9dca2368f9d8470 

but, problem convert byte array usable format verify signature java.security.publickey receiver.

in general, there solution verify signature without converting byte array? in other word, problem verify signature sender's public key, using method.

but, problem convert byte array usable format verify signature java.security.publickey receiver.

you can solve problem way:

public static ecpublickey genecpubkey() throws exception {     keyfactory factory = keyfactory.getinstance("ecdsa", "bc");     java.security.publickey ecpublickey = (ecpublickey) factory             .generatepublic(new x509encodedkeyspec(helper                     .tobyte(ecremotepubkey))); // helper.tobyte(ecremotepubkey)) java.security.publickey#getencoded()     return (ecpublickey) ecpublickey; } 

note that, need bouncycastle provider that.

but question remains, how generate private key?

public keypair eckeypairgenerator(string curvename) throws exception {     keypair keypair;     keypairgenerator keypairgenerator = keypairgenerator.getinstance(             "ecdsa", "bc");     ecgenparameterspec ecgenparameterspec = new ecgenparameterspec(             curvename);     keypairgenerator.initialize(ecgenparameterspec, new securerandom());     keypair = keypairgenerator.generatekeypair();     java.security.publickey ecpublickey = (ecpublickey) keypair.getpublic();     system.out.println("java ec publickey: "             + helper.tohex(ecpublickey.getencoded()));      // write private key file. testing purpose     fileoutputstream fileoutputstream = new fileoutputstream(             "ecprivatekey.key");     objectoutputstream objectoutputstream = new objectoutputstream(             fileoutputstream);     objectoutputstream.writeobject(keypair.getprivate());     objectoutputstream.close();     return keypair; } 

i have full running code ec sign/verify in github. can take better understanding.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -