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,390 points
638 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

How is a transaction public key related to original public?

The details like account balance, nonce etc., ...READ MORE

answered Jul 24, 2018 in Blockchain by digger
• 26,740 points
670 views
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
1,712 views
0 votes
1 answer

In assymetric cryptography how and when the sender will share the public key with the receiver?

Consider this example: Suppose Bob wants to send ...READ MORE

answered Jul 3, 2019 in Blockchain by Sheila
740 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
1,883 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,400 views
+2 votes
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
1,998 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
732 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP