0

I am trying to find the lenghth of the ECC public key. I read that the public key of ECC is about 64 bytes. I tried to test this my self but the result is different than the common one. Provided that I am using this specification (secp256k1). The code which I used to calculate the length of the public key is given below.

byte publicKeyLength [] =pk.getEncoded();

System.out.println("The Length of PK is " +publicKeyLength.length); // it prints 311 byes?

As I read in this forum it should be 64 bytes. But Why when I tested it, it gives 311 bytes?

Tahani
  • 43
  • 5

1 Answers1

1

If you refer to rfc5480 you will see that the bytes that you got from PublicKey::getEncoded method is actually DER encoded SubjectPublicKeyInfo :

SubjectPublicKeyInfo  ::=  SEQUENCE  {
       algorithm         AlgorithmIdentifier,
       subjectPublicKey  BIT STRING
}

And the subjectPublicKey will be DER OCTET String encoded ECPublicKey and this key consists of first byte which is the on that determines that key is compressed or not and then the X, Y coordinates of the public key EC point.

And the length of X, Y fields depends on which curve the point is defined. For your curve it will be 32 bytes for each coordinate. Therefore in uncompressed form the raw key will have 65 bytes (1 + 32 + 32). The difference between compressed and uncompressed forms has been explained in this SO question.

Community
  • 1
  • 1
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
  • Thanks, Can you help me to get the 65 byte which represent Public key. i.e. rather than using getencoded method what should I use? – Tahani Jun 26 '19 at 10:34
  • You can use some external library like BouncCastle and it's API for DER/ASN1 encodings to retrive the objects from der encoded byte structure as you wish. – Michał Krzywański Jun 26 '19 at 10:53