How to recovery public key asn.1 structure from ECPoint?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to recovery public key asn.1 structure from ECPoint?

Gsealy
hi all,
     I want to calculate a public key from private key, and recovery it to asn.1 structure.
But the asn.1 structure has something wrong(see OUTPUT Hex String)
anyone who can help me, plz.

```java

/* test privkey */
private static final String privKey = "308193020100301306072a8648ce3d020106082a8648ce3d0301070479307702010104204833b1bae3464a7495896b9a332cdaa31f4b3b072cc79336d6b6270604d2f7aca00a06082a8648ce3d030107a144034200042f2cc7e74cdd56bfb6b30b92ec59eea14011973ebadb31cc04b151f9eb15d5ebc5d28fd940889c3b0eb234c1bd7d5fbf4c6d2f1692cfd41c37a361a19e6f32b9";

/**
* generate public from private key
*/
public static PublicKey getPublicKey(PrivateKey privateKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("EC", new BouncyCastleProvider());
ECPrivateKeySpec privSpec = keyFactory.getKeySpec(privateKey, ECPrivateKeySpec.class);
ECParameterSpec params = privSpec.getParams();
// calculate public key
ECPoint q = params.getG().multiply(privSpec.getD());
ECPublicKeySpec pubSpec = new ECPublicKeySpec(q.normalize(), params);
return keyFactory.generatePublic(pubSpec);
}

```

==== OUTPUT Hex ====

original ---> 3059301306072a8648ce3d020106082a8648ce3d030107034200042f2cc7e74cdd56bfb6b30b92ec59eea14011973ebadb31cc04b151f9eb15d5ebc5d28fd940889c3b0eb234c1bd7d5fbf4c6d2f1692cfd41c37a361a19e6f32b9
calculate --> 308201333081ec06072a8648ce3d02013081e0020101302c06072a8648ce3d0101022100ffffffff00000001000000000000000000000000ffffffffffffffffffffffff30440420ffffffff00000001000000000000000000000000fffffffffffffffffffffffc04205ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b0441046b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5022100ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551020101034200042f2cc7e74cdd56bfb6b30b92ec59eea14011973ebadb31cc04b151f9eb15d5ebc5d28fd940889c3b0eb234c1bd7d5fbf4c6d2f1692cfd41c37a361a19e6f32b9

_____________
Thanks,
Gsealy

Reply | Threaded
Open this post in threaded view
|

Re: How to recovery public key asn.1 structure from ECPoint?

David Hook-3

You need to maintain the use of a named curve parameter in the second structure. The first key has been encoded using a named curve, not explicit parameters.

Regards,

David

On 21/1/19 8:20 pm, J Gsealy wrote:
hi all,
     I want to calculate a public key from private key, and recovery it to asn.1 structure.
But the asn.1 structure has something wrong(see OUTPUT Hex String)
anyone who can help me, plz.

```java

/* test privkey */
private static final String privKey = "308193020100301306072a8648ce3d020106082a8648ce3d0301070479307702010104204833b1bae3464a7495896b9a332cdaa31f4b3b072cc79336d6b6270604d2f7aca00a06082a8648ce3d030107a144034200042f2cc7e74cdd56bfb6b30b92ec59eea14011973ebadb31cc04b151f9eb15d5ebc5d28fd940889c3b0eb234c1bd7d5fbf4c6d2f1692cfd41c37a361a19e6f32b9";

/**
* generate public from private key
*/
public static PublicKey getPublicKey(PrivateKey privateKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("EC", new BouncyCastleProvider());
ECPrivateKeySpec privSpec = keyFactory.getKeySpec(privateKey, ECPrivateKeySpec.class);
ECParameterSpec params = privSpec.getParams();
// calculate public key
ECPoint q = params.getG().multiply(privSpec.getD());
ECPublicKeySpec pubSpec = new ECPublicKeySpec(q.normalize(), params);
return keyFactory.generatePublic(pubSpec);
}

```

==== OUTPUT Hex ====

original ---> 3059301306072a8648ce3d020106082a8648ce3d030107034200042f2cc7e74cdd56bfb6b30b92ec59eea14011973ebadb31cc04b151f9eb15d5ebc5d28fd940889c3b0eb234c1bd7d5fbf4c6d2f1692cfd41c37a361a19e6f32b9
calculate --> 308201333081ec06072a8648ce3d02013081e0020101302c06072a8648ce3d0101022100ffffffff00000001000000000000000000000000ffffffffffffffffffffffff30440420ffffffff00000001000000000000000000000000fffffffffffffffffffffffc04205ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b0441046b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5022100ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551020101034200042f2cc7e74cdd56bfb6b30b92ec59eea14011973ebadb31cc04b151f9eb15d5ebc5d28fd940889c3b0eb234c1bd7d5fbf4c6d2f1692cfd41c37a361a19e6f32b9

_____________
Thanks,
Gsealy