<p>Instead of compressing 'by hand' you can have OpenSSL do it:</p>
<pre><code>openssl ec -in privatekey.pem -pubout -conv_form compressed -outform der | tail -c 33 | xxd -p -c 33 &gt;compressedpub.hex
</code></pre>
<p>And .pem is <em>usually</em> used to mean PEM format, while the files you are creating are totally not at all or in any way PEM format, so that's very misleading and confusing. I used .hex for my example, but there are other reasonable possibilities.</p>

CC BY-SA 4.0
2022-01-07T01:43:04.673Z
2022-01-07T01:43:04.673Z
dave_thompson_085
<p>You just check if Y is divisible by 2 (i.e Y % 2 == 0). It's the default parity concept…</p>

CC BY-SA 4.0
2022-01-06T18:07:46.660Z
2022-01-06T18:07:46.660Z
Davidson Souza
<p>I generate the private and the public keys in Shell like that:</p>
<pre><code>openssl ecparam -name secp256k1 -rand /dev/random -genkey -noout -out private-key.pem
openssl ec -in private-key.pem -outform DER | tail -c +8 | head -c 32 | xxd -p -c 32 &gt; bitcoin_private_key.pem
openssl ec -in private-key.pem -pubout -outform DER | tail -c 65 | xxd -p -c 65 &gt; bitcoin_public_key.pem
</code></pre>
<p>Then I can take the public key X with:</p>
<pre><code>head -c 66 bitcoin_public_key.pem | tail -c 64
</code></pre>
<p>And the last byte of Y with:</p>
<pre><code>tail -c 3 bitcoin_public_key.pem
</code></pre>
<p>The compressed public key must be <code>0x02 + X</code> for an even last byte of <code>Y</code> and <code>0x03 + X</code> for an odd one.</p>
<p>How to check if the last Y's byte is odd or even?</p>

<public-key><openssl><bash>
How to Compress Public Key with OpenSSL / Bash
Miroslav Popov
# How to Compress Public Key with OpenSSL / Bash

(Asked by: username_0 on 2022-01-06)

I generate the private and the public keys in Shell like that:

```
openssl ecparam -name secp256k1 -rand /dev/random -genkey -noout -out private-key.pem
openssl ec -in private-key.pem -outform DER | tail -c +8 | head -c 32 | xxd -p -c 32 > bitcoin_private_key.pem
openssl ec -in private-key.pem -pubout -outform DER | tail -c 65 | xxd -p -c 65 > bitcoin_public_key.pem

```

Then I can take the public key X with:

```
head -c 66 bitcoin_public_key.pem | tail -c 64

```

And the last byte of Y with:

```
tail -c 3 bitcoin_public_key.pem

```

The compressed public key must be `0x02 + X` for an even last byte of `Y` and `0x03 + X` for an odd one.

How to check if the last Y's byte is odd or even?

**Tags:** public-key, openssl, bash


---

## 2 answers

### Answer by username_1 on 2022-01-07. Score: 2 | Accepted answer

Instead of compressing 'by hand' you can have OpenSSL do it:

```
openssl ec -in privatekey.pem -pubout -conv_form compressed -outform der | tail -c 33 | xxd -p -c 33 >compressedpub.hex

```

And .pem is *usually* used to mean PEM format, while the files you are creating are totally not at all or in any way PEM format, so that's very misleading and confusing. I used .hex for my example, but there are other reasonable possibilities.


---

### Answer by username_2 on 2022-01-06. Score: 0

You just check if Y is divisible by 2 (i.e Y % 2 == 0). It's the default parity concept…


---

