1

I am trying to run the code in this pdf. For example, for ECCKeyGeneration, I get the following output instead of Figure 1 in the pdf:

sun.security.ec.ECPrivateKeyImpl@58b6
Sun EC public key, 192 bits
  public x coord: 4812174841545539052388802454891896756539688756781766645984
  public y coord: 1161396487043052042009627836016170768650083444786081272028
  parameters: secp192r1 [NIST P-192, X9.62 prime192v1] (1.2.840.10045.3.1.1)

The private key doesn't printed to console/screen. Instead, it says "sun.security.ec.ECPrivateKeyImpl@blabla" as you can see above. What could be the reason for this situation?

Here is the code if you like to test:

import java.security.*;
import java.security.spec.*;

public class ECCKeyGeneration {

    public static void main(String[] args) throws Exception {
        KeyPairGenerator kpg;
        kpg = KeyPairGenerator.getInstance("EC","SunEC");
        ECGenParameterSpec ecsp;
        ecsp = new ECGenParameterSpec("secp192r1");
        kpg.initialize(ecsp);

        KeyPair kp = kpg.genKeyPair();
        PrivateKey privKey = kp.getPrivate();
        PublicKey pubKey = kp.getPublic();

        System.out.println(privKey.toString());
        System.out.println(pubKey.toString());
    }

}
Mehmed
  • 2,880
  • 4
  • 41
  • 62
  • Best not to confuse EC with ECC as the later means something very different. – Peter Lawrey Jun 24 '14 at 09:58
  • Nearest tag available, that's why I chose it :/ – Mehmed Jun 24 '14 at 10:00
  • Do you understand that `KeyPairGenerator.genKeyPair()` generates a *random* key pair, where *random* means that the value will be different each time you run it? – Oleg Estekhin Jun 24 '14 at 10:00
  • FYI http://en.wikipedia.org/wiki/Error_detection_and_correction – Peter Lawrey Jun 24 '14 at 10:02
  • Yes, I know, but I cannot get Figure 1 of the pdf. – Mehmed Jun 24 '14 at 10:02
  • Do not use toString(). Use the getEncoded() methode to get the byte[] of your private key. Now you can try to print it or convert it to a string (maybe Base64). – HectorLector Jun 24 '14 at 10:11
  • I tried that. I add "byte[] privateKeyBytes = privKey.getEncoded(); System.out.println(privateKeyBytes.toString());" at the end. I get one of these three values for each run: "[B@19214b1", "[B@18c8aea" and "[B@d19cdf" – Mehmed Jun 24 '14 at 10:20

3 Answers3

1

Your Problem is with the toString() methode of the java PrivateKey class. toString() does not necessarily returns all the data in your object, just a string representation.

So in your case you could use the getEncoded() methode. Note that your byte[] might contain unprintable characters, so you may need to convert it in a readable form f.e. Base64.

PrivateKey privKey = kp.getPrivate();
byte[] data = privKey.getEncoded();

//first methode, convert to string
String privKeyString = new String(data);
System.out.println(privKeyString);

//second methode, print all byte values
for(byte value : data)
   System.out.println(value);

See also print byte array

Community
  • 1
  • 1
HectorLector
  • 1,851
  • 1
  • 23
  • 33
1

Normally private key values are not printed to screen. Hence there is little sense to provide a toString() for ECPrivateKey (a sub-class of PrivateKey).Printing out private key values is of course not safe.

It is of course possible to print out the secret part of the private key; printing out the other parameters of secp192r1 makes little sense. You can easily retrieve them from standard documents from NIST or Certicom if required.

ECPrivateKey ecPrivKey = (ECPrivateKey) eckp.getPrivate();
System.out.println(ecPrivKey.getS().toString(16));

Note that you are better off printing out a hash over the private key value S if you just need this for verifying that the right private key is used.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
1

Also you can use:

System.out.println(Arrays.toString(kp.getPrivate().getEncoded()));
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114