0

I have generated ECC public and private key using secp192r1 curve. I get 75 for public and 125 for private encoded key array length. Why private key is longer than public key? Why private key is not longer two times than public? Why private key is not 192 bits = 24 bytes because of secp192r1?

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp192r1");

KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(ecSpec, new SecureRandom());
KeyPair pair = g.generateKeyPair();

System.out.println(pair.getPublic().getEncoded().length);
System.out.println(pair.getPrivate().getEncoded().length);
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
  • 1
    The encoded forms contain more than just the bare keys. They are encoded versions of more complex ASN.1 structures that have additional info. Also, points on an elliptic curve may be encoded with either the full (X,Y) pair or as a shorter "compressed" form containing just the X coordinate plus a sign bit. In your case the points are encoded using the full (X,Y) pair. The private key structure includes the public key as one of its components. – President James K. Polk May 27 '16 at 20:36
  • How to get 24 bytes key which can be reused in C lib micro-ecc? – Justinas Jakavonis May 28 '16 at 07:37
  • 1
    Cast the private key to ECPrivateKey and call the getS method. – President James K. Polk May 28 '16 at 12:04
  • ECPrivateKey privateKey = (ECPrivateKey)pair.getPrivate(); privateKey.getS(); returns BigInteger. Is there any way to get byte array? – Justinas Jakavonis May 31 '16 at 11:40
  • How to get key which can be used in this library https://github.com/kmackay/micro-ecc/blob/master/uECC.h? – Justinas Jakavonis May 31 '16 at 13:55

1 Answers1

0

Code fragment below outputs 24 bytes private key:

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp192r1");

KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(spec, new SecureRandom());
KeyPair pair = g.generateKeyPair();
ECPrivateKey ecPrivateKey = (ECPrivateKey)pair.getPrivate();

System.out.println(ecPrivateKey.getS().toByteArray().length);
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114