ECC key pair - how to print private key?

nistelrooy41001662

I implement "Key Pair Generation" using secp192r1 curve. But private key did not display in string form like public key.

enter image description here

Here is my code:

package lam.bk;
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(pubKey.toString());
        System.out.println(privKey.toString()); 
    }
}
Justinas Jakavonis

The code below will output 24 bytes private key for secp192r1 curve:

private String getPrivateKeyAsHex(PrivateKey privateKey) {

    ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
    byte[] privateKeyBytes = new byte[24];
    writeToStream(privateKeyBytes, 0, ecPrivateKey.getS(), 24);

    return Hex.toHexString(privateKeyBytes);
}

private void writeToStream(byte[] stream, int start, BigInteger value, int size) {
    byte[] data = value.toByteArray();
    int length = Math.min(size, data.length);
    int writeStart = start + size - length;
    int readStart = data.length - length;
    System.arraycopy(data, readStart, stream, writeStart, length);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use ECC private key with Java 1.6?

From Dev

How to use ECC private key with Java 1.6?

From Dev

ECC private key is longer than public key

From Dev

How to validate a public and private key pair in Java

From Dev

How to replace the ssh private-public key pair?

From Dev

Get CryptoException.ILLEGAL_VALUE when generating ECC key pair

From Dev

Get CryptoException.ILLEGAL_VALUE when generating ECC key pair

From Dev

How does public/private key cryptography work, who generates the key pair?

From Dev

Generate an RSA public / private key pair

From Dev

Generate a public/private key pair in PEM format

From Java

How to convert a private key to an RSA private key?

From Dev

In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

From Dev

In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

From Dev

Getting private key from ServiceResource create_key_pair

From Dev

How to rsync with a private key?

From Dev

GnuPG generating public/private key pair where public key and Private key are same and not different

From Dev

How To Check key in key pair is available or not?

From Dev

How to initialize a EC key pair with an existing key pair in JavaCard?

From Dev

How to store private key in Key Container?

From Dev

Unattended GnuPG Key Generation for ECC

From Java

Importing the private-key/public-certificate pair in the Java KeyStore

From Dev

Can I change the filename of my ssh public/private key pair?

From Dev

Setting up Public/Private Key Pair for Linux Login (not SSH)

From Dev

How to retrieve passphrase for private key?

From Dev

How to Export Private Key For ECDiffieHellmanCng

From Dev

How to find Private Key Location

From Dev

How to retrieve passphrase for private key?

From Dev

How to encrypt a file with private key

From Dev

How to install ssh private key?

Related Related

  1. 1

    How to use ECC private key with Java 1.6?

  2. 2

    How to use ECC private key with Java 1.6?

  3. 3

    ECC private key is longer than public key

  4. 4

    How to validate a public and private key pair in Java

  5. 5

    How to replace the ssh private-public key pair?

  6. 6

    Get CryptoException.ILLEGAL_VALUE when generating ECC key pair

  7. 7

    Get CryptoException.ILLEGAL_VALUE when generating ECC key pair

  8. 8

    How does public/private key cryptography work, who generates the key pair?

  9. 9

    Generate an RSA public / private key pair

  10. 10

    Generate a public/private key pair in PEM format

  11. 11

    How to convert a private key to an RSA private key?

  12. 12

    In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

  13. 13

    In Java, how do I decrypt using the private key from an X509 certificate (public/private key pair) inside a JKS keystore?

  14. 14

    Getting private key from ServiceResource create_key_pair

  15. 15

    How to rsync with a private key?

  16. 16

    GnuPG generating public/private key pair where public key and Private key are same and not different

  17. 17

    How To Check key in key pair is available or not?

  18. 18

    How to initialize a EC key pair with an existing key pair in JavaCard?

  19. 19

    How to store private key in Key Container?

  20. 20

    Unattended GnuPG Key Generation for ECC

  21. 21

    Importing the private-key/public-certificate pair in the Java KeyStore

  22. 22

    Can I change the filename of my ssh public/private key pair?

  23. 23

    Setting up Public/Private Key Pair for Linux Login (not SSH)

  24. 24

    How to retrieve passphrase for private key?

  25. 25

    How to Export Private Key For ECDiffieHellmanCng

  26. 26

    How to find Private Key Location

  27. 27

    How to retrieve passphrase for private key?

  28. 28

    How to encrypt a file with private key

  29. 29

    How to install ssh private key?

HotTag

Archive