<p>In case some one want to implement compression in their code:</p>
<pre><code>private static final String EVEN = &quot;02&quot;;
private static final String ODD = &quot;03&quot;;

public String compressPublicKey(String toCompress) {
    if (Integer.parseInt(toCompress.substring(128, 130), 16) % 2 == 0)
        return  EVEN + toCompress.substring(2, 66);
    return ODD + toCompress.substring(2, 66);
} 
</code></pre>

CC BY-SA 4.0
2019-01-09T15:54:14.153Z
2020-12-20T12:17:17.700Z
2020-12-20T12:17:17.700Z
Roie Beck
<pre><code>Address address = new Address(params, dk.decompress().getPubKeyHash());
</code></pre>

<p>where dk is the DeterministicKey</p>

<p>or </p>

<pre><code>key.decompress().getPubKey();
</code></pre>

<p>where key is the ECKey</p>

CC BY-SA 3.0
2018-02-09T20:40:26.643Z
2018-02-09T20:40:26.643Z
bitsabhi
<pre><code>private static final String EVEN = "02";
private static final String ODD = "03";

public String compressPublicKey(String toCompress) {
    if (Integer.parseInt(toCompress.substring(128, 130), 16) % 2 == 0)
        return  EVEN + toCompress.substring(2, 66);
    return ODD + toCompress.substring(2, 66);
} 1
โหวต
Address address = new Address(params, dk.decompress().getPubKeyHash())key.decompress().getPubKey();
</code></pre>

CC BY-SA 4.0
2019-02-09T09:16:50.520Z
2019-02-14T05:34:24.507Z
2019-02-14T05:34:24.507Z
Natthaphong Somsamai
<p>use LegacyAddress:</p>
<pre><code>public static String getPublicAddress(String input) {
    ECKey address = createAddress(input.getBytes());

    byte[] hash160 = RIPEMD160(SHA256(address.getPubKey()));

    LegacyAddress legacyAddress = LegacyAddress.fromPubKeyHash(MainNetParams.get(), hash160);
    return legacyAddress.toBase58();
}
</code></pre>

CC BY-SA 4.0
2021-01-20T21:19:59.137Z
2021-01-20T21:19:59.137Z
pwipo
<p>Here some java code. What i need to change to get compressed public address in getPublicAddress?</p>

<pre><code>import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.params.MainNetParams;
import org.spongycastle.crypto.digests.RIPEMD160Digest;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil {

    public static byte[] RIPEMD160(byte[] input) {
        RIPEMD160Digest d = new RIPEMD160Digest();

        d.update(input, 0, input.length);

        byte[] o = new byte[d.getDigestSize()];

        d.doFinal(o, 0);

        return o;
    }

    public static byte[] SHA256(byte[] input) {
        byte[] hash = null;

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(input);
            hash = md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return hash;
    }

    public static ECKey createAddress(byte[] secret) {
        byte[] hash = SHA256(secret);

        ECKey key = new ECKey(hash, null);

        return key;
    }

    public static String getPublicAddress(String input) {
        ECKey address = createAddress(input.getBytes());

        byte[] hash160 = RIPEMD160(SHA256(address.getPubKey()));

        Address addr = new Address(MainNetParams.get(), hash160);

        return addr.toString();
    }

}
</code></pre>

CC BY-SA 3.0
2018-01-10T13:26:02.523Z
this question is a bit "non logic". Normally one does not convert between  uncompressed and compressed pubkeys. The danger is in creating unspendable outputs. Instead, one would derive compressed or uncompressed pubkeys from the corresponding private keys (here people sometimes talk about compressed or uncompressed WIF keys). On WIF keys there was just a thread here: https://bitcoin.stackexchange.com/questions/68065/private-key-to-wif-compressed-which-one-is-correct
pebwindkraft
CC BY-SA 3.0
2018-01-10T14:51:58.890Z
Why non logic? That is the question - to derive compressed pubkey (in getPublicAddress?) from corresponding private key from createAddress.
krab
CC BY-SA 3.0
2018-01-10T15:02:19.117Z
Need to get "new Eckey" with compressed marker, then compressed WIF public key in getPublicAddress.
krab
CC BY-SA 3.0
2018-01-10T21:02:54.030Z
ah, didn't get this from the question :-) To get to the compressed pubkey you need to have a "compressed WIF" key. This is derived from private key, with a hex "01" at the end, and base58 encoded. I am not programming in Java, and don't have the ECKey library details, especially as I don't see any base58 encoding - so ECKey createAddress() returns your key, which I assume is the 32 Bytes hex key format. Then the function address.getPubKey() seems to derive a standard pubkey (uncompressed). Maybe the library has address.getCompressedPubKey() ?
pebwindkraft
CC BY-SA 3.0
2018-01-10T21:56:42.903Z
no, bullshit... did some re-reading. The WIF-c key is only needed to "advise" the wallet, to create privkeys in compressed format. At the end,  when uncompressed pubkey is created, it's hex representation is "04" + "x" + "y". A compressed pubkey is then a "02" (for even y) or "03" (uneven) + "x". I leave it to the JAVA experts of these libraries, where to find the correct code ...
pebwindkraft
<private-key><public-key><bitcoinj><java>
How to get compressed public key with ECKey
krab
# How to get compressed public key with ECKey

(Asked by: username_0 on 2018-01-10)

Here some java code. What i need to change to get compressed public address in getPublicAddress?

```
import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.params.MainNetParams;
import org.spongycastle.crypto.digests.RIPEMD160Digest;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil {

    public static byte[] RIPEMD160(byte[] input) {
        RIPEMD160Digest d = new RIPEMD160Digest();

        d.update(input, 0, input.length);

        byte[] o = new byte[d.getDigestSize()];

        d.doFinal(o, 0);

        return o;
    }

    public static byte[] SHA256(byte[] input) {
        byte[] hash = null;

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(input);
            hash = md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return hash;
    }

    public static ECKey createAddress(byte[] secret) {
        byte[] hash = SHA256(secret);

        ECKey key = new ECKey(hash, null);

        return key;
    }

    public static String getPublicAddress(String input) {
        ECKey address = createAddress(input.getBytes());

        byte[] hash160 = RIPEMD160(SHA256(address.getPubKey()));

        Address addr = new Address(MainNetParams.get(), hash160);

        return addr.toString();
    }

}

```

**Tags:** private-key, public-key, bitcoinj, java

> **Comments:**
> **username_1**: this question is a bit "non logic". Normally one does not convert between  uncompressed and compressed pubkeys. The danger is in creating unspendable outputs. Instead, one would derive compressed or uncompressed pubkeys from the corresponding private keys (here people sometimes talk about compressed or uncompressed WIF keys). On WIF keys there was just a thread here: https://bitcoin.stackexchange.com/questions/68065/private-key-to-wif-compressed-which-one-is-correct  
> **username_0**: Why non logic? That is the question - to derive compressed pubkey (in getPublicAddress?) from corresponding private key from createAddress.  
> **username_0**: Need to get "new Eckey" with compressed marker, then compressed WIF public key in getPublicAddress.  
> **username_1**: ah, didn't get this from the question :-) To get to the compressed pubkey you need to have a "compressed WIF" key. This is derived from private key, with a hex "01" at the end, and base58 encoded. I am not programming in Java, and don't have the ECKey library details, especially as I don't see any base58 encoding - so ECKey createAddress() returns your key, which I assume is the 32 Bytes hex key format. Then the function address.getPubKey() seems to derive a standard pubkey (uncompressed). Maybe the library has address.getCompressedPubKey() ?  
> **username_1**: no, bullshit... did some re-reading. The WIF-c key is only needed to "advise" the wallet, to create privkeys in compressed format. At the end,  when uncompressed pubkey is created, it's hex representation is "04" + "x" + "y". A compressed pubkey is then a "02" (for even y) or "03" (uneven) + "x". I leave it to the JAVA experts of these libraries, where to find the correct code ...  

---

## 4 answers

### Answer by username_2 on 2019-01-09. Score: 1

In case some one want to implement compression in their code:

```
private static final String EVEN = "02";
private static final String ODD = "03";

public String compressPublicKey(String toCompress) {
    if (Integer.parseInt(toCompress.substring(128, 130), 16) % 2 == 0)
        return  EVEN + toCompress.substring(2, 66);
    return ODD + toCompress.substring(2, 66);
} 

```


---

### Answer by username_3 on 2018-02-09. Score: 1

```
Address address = new Address(params, dk.decompress().getPubKeyHash());

```

where dk is the DeterministicKey

or

```
key.decompress().getPubKey();

```

where key is the ECKey


---

### Answer by username_4 on 2019-02-09. Score: 0

```
private static final String EVEN = "02";
private static final String ODD = "03";

public String compressPublicKey(String toCompress) {
    if (Integer.parseInt(toCompress.substring(128, 130), 16) % 2 == 0)
        return  EVEN + toCompress.substring(2, 66);
    return ODD + toCompress.substring(2, 66);
} 1
โหวต
Address address = new Address(params, dk.decompress().getPubKeyHash())key.decompress().getPubKey();

```


---

### Answer by username_5 on 2021-01-20. Score: 0

use LegacyAddress:

```
public static String getPublicAddress(String input) {
    ECKey address = createAddress(input.getBytes());

    byte[] hash160 = RIPEMD160(SHA256(address.getPubKey()));

    LegacyAddress legacyAddress = LegacyAddress.fromPubKeyHash(MainNetParams.get(), hash160);
    return legacyAddress.toBase58();
}

```


---

