android - Generating PublicKey from x and y values of elliptic curve point -


i trying generate shared secret in app this:

public static byte[] generatesharedsecret(privatekey privatekey publickey publickey) {     keyagreement keyagreement = keyagreement.getinstance("ecdh", "sc");     keyagreement.init(privatekey);     keyagreement.dophase(publickey, true);     return keyagreement.generatesecret(); }  

this working fine, publickey use here should coming backend.

the backend sends me x , y value of point on elliptic curve , supposed generate publickey that. can't figure out! how can create publickey instance 2 values?

it's quite simple! need 1 more thing besides x , y values. need ecparameterspec! ecparameterspec describes elliptic curve using , app has use same ecparameterspec backend does!


with x , y values can create ecpoint instance , ecparameterspec can create ecpublickeyspec:

ecparameterspec ecparameters = ...; biginteger x = ...; biginteger y = ...;  ecpoint ecpoint = new ecpoint(x, y); ecpublickeyspec keyspec = new ecpublickeyspec(ecpoint, ecparameters); 

and ecpublickeyspec can generate publickey using keyfactory:

keyfactory keyfactory = keyfactory.getinstance("ec"); publickey publickey = keyfactory.generatepublic(keyspec); 

you can find more information topic here.


Comments