0
package com.project;
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());
  }
}

[2] This is the code for elliptic curve cryptography for public and private key generation but when I executed this code ,showing only the public key but not the private key,so please help me out and let me know ,what to do to generate the private key!!

  • Write code that generates the private key? Since we have no idea what that code does, we don't know the classes, we can just assume the implementation of getPrivate() is something like: return ""; – Stultuske May 18 '15 at 12:14
  • 1
    @Stultuske please calm down... `KeyPair` is a standart java object... check it [here](http://docs.oracle.com/javase/7/docs/api/java/security/KeyPair.html) – Jordi Castilla May 18 '15 at 12:18
  • Find also [here](http://docs.oracle.com/javase/7/docs/api/java/security/KeyPair.html#getPrivate()) method `getPrivate()` – Jordi Castilla May 18 '15 at 12:19
  • when executed ,showing --> Main.java:4: error: class ECCKeyGeneration is public, should be declared in a file named ECCKeyGeneration.java public class ECCKeyGeneration { ^ 1 error //so what to do?? – AJAY KUMAR May 18 '15 at 12:37
  • Name your file as ECCKeyGeneration.java, with a try catch block.Then compile and run file. I tried your code, it works & gives both private and Public Keys. – Arun Xavier May 18 '15 at 12:40
  • when executed using Eclipse ,showing-->Sun EC public key, 192 bits public x coord: 2968371464961728379253096739591505362401590116472501032440 public y coord: 5335329213657324346806403557586964569179386657247522228810 parameters: secp192r1 [NIST P-192, X9.62 prime192v1] (1.2.840.10045.3.1.1) //but nothing about private key – AJAY KUMAR May 18 '15 at 12:55

1 Answers1

0
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.ECGenParameterSpec;

public class ECCKeyGeneration {

    public static void main(String[] args)  {
        try {
            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());
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

I've just tested the above code.

Output

Sun EC private key, 192 bits private value: 3248833611418544793834748156439256292267803494576663573112
parameters: secp192r1 [NIST P-192, X9.62 prime192v1] (1.2.840.10045.3.1.1) Sun EC public key, 192 bits public x coord: 5122655651118956061783347731888893733494103991283417332818 public y coord: 223043343028867724454216740788693823451155477884918709166
parameters: secp192r1 [NIST P-192, X9.62 prime192v1] (1.2.840.10045.3.1.1)

Are you getting the same output?

Arun Xavier
  • 763
  • 8
  • 47
  • sun.security.ec.ECPrivateKeyImpl@ffffdf17 Sun EC public key, 192 bits public x coord: 2986437850845590154783978576780601064801052697129613842435 public y coord: 4073070570923625270823330364164716679062682143264492286968 parameters: secp192r1 [NIST P-192, X9.62 prime192v1] (1.2.840.10045.3.1.1). @xavy:I AM NOT GETTING THE PRIVATE VALUE AS YOU MENTIONED ABOVE. – AJAY KUMAR May 21 '15 at 07:20
  • after private key , < Impl@ffffdf17 > is coming but not the value as you mentioned above <3248833611418544793834748156439256292267803494576663573112> – AJAY KUMAR May 21 '15 at 07:28