Public key encoding changes

0 votes

I've generated a public key using elliptic curves, but whenever I get the encoding of the key, it changes. I would like to use the encoding for a public address of a blockchain I am implementing. See below

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

class Scratch {
    public static void main(String[] args) throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
        ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1");
        keyGen.initialize(ecSpec);
        KeyPair kp = keyGen.generateKeyPair();

        PublicKey k = kp.getPublic();
        byte[] one = k.getEncoded();
        byte[] two = k.getEncoded();

        System.out.println(one);
        System.out.println(two);

    }
}

With output

[B@4eec7777
[B@3b07d329

Does anyone know why this is happening? My guess is that it is the expected behavior and I am just misunderstanding something fundamental here.

Sep 19, 2018 in Blockchain by sabby
• 4,350 points
33 views

1 answer to this question.

0 votes

You are printing the memory address of the byte[], not the key itself. You can't "print" a byte array because it is binary data, you need to encode it somehow first. Hex and base64 are both good encodings for displaying binary data. For memory, other blockchain implementations like to use base58.

Here is an example using base64:

String b64Str = Base64.getEncoder().encodeToString(one);
System.out.println(b64Str);
answered Sep 19, 2018 by Christine
• 15,790 points

Related Questions In Blockchain

0 votes
2 answers
0 votes
2 answers

Creating a private/public key with 64 characters that are already known using bitcoinjs

//public-key var address = eckey.getBitcoinAddress().toString(); var privateKeyBytesCompressed = privateKeyBytes.slice(0); ...READ MORE

answered Sep 10, 2018 in Blockchain by Misty
51 views
0 votes
1 answer
+1 vote
5 answers

Difference between internal-external, private-public in function

External: These functions are part of the ...READ MORE

answered Apr 7, 2018 in Blockchain by Shashank
• 10,350 points
1,341 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
138 views
0 votes
1 answer
+1 vote
1 answer

Blockchain declining my API key and I cannot understand why.

It's quite easy. Request the API key using ...READ MORE

answered Apr 3, 2018 in Blockchain by Christine
• 15,790 points
211 views
0 votes
1 answer

Is it possible to claim BCH without knowing private key?

You can't claim BCH without knowing private ...READ MORE

answered Sep 5, 2018 in Blockchain by Christine
• 15,790 points
55 views