# Extract a public key

To extract the public key from a private key, run `openssl pkey -in private.key -pubout -out public.pem`. From a certificate, run `openssl x509 -in certificate.crt -noout -pubkey -out public.pem`. From a CSR, run `openssl req -in request.csr -noout -pubkey -out public.pem`. All three produce the same `-----BEGIN PUBLIC KEY-----` SubjectPublicKeyInfo structure, which is why hashing the output is the reliable way to check whether a key, certificate and CSR all belong to the same key pair.

Source: https://certguru.io/openssl/extract-public-key/
Last updated: 2026-08-19
Commands verified against OpenSSL 3.5.7.

---

## From a private key

```bash
openssl pkey -in private.key -pubout -out public.pem
```

*What the result looks like*

```bash
head -n1 public.pem
```

Expected output:

```
-----BEGIN PUBLIC KEY-----
```

## From a certificate

```bash
openssl x509 -in certificate.crt -noout -pubkey -out public.pem
```

## From a CSR

```bash
openssl req -in request.csr -noout -pubkey -out public.pem
```

## Why this is the useful primitive

All three commands emit the same structure for the same key pair. That makes a hash of the output a
clean equality test — and it works for RSA, EC and Ed25519 alike, unlike the older `-modulus`
comparison which is RSA-only:

*Do these three files belong together?*

```bash
openssl pkey -in private.key    -pubout | openssl sha256
openssl req  -in request.csr    -noout -pubkey | openssl sha256
openssl x509 -in certificate.crt -noout -pubkey | openssl sha256
```

Expected output:

```
SHA2-256(stdin)= 9d4c1f2e8a3b...
SHA2-256(stdin)= 9d4c1f2e8a3b...
SHA2-256(stdin)= 9d4c1f2e8a3b...
```

Three identical hashes mean the key, the request and the issued certificate are all one key pair.
See [check whether a key matches a certificate](/openssl/check-key-matches-certificate/).

## Inspecting a public key

```bash
openssl pkey -pubin -in public.pem -noout -text
```

Expected output:

```
Public-Key: (2048 bit)
Modulus:
    00:c4:1f:...
Exponent: 65537 (0x10001)
```

Note `-pubin` — without it OpenSSL expects a private key and fails.

## DER output

```bash
openssl pkey -in private.key -pubout -outform der -out public.der
```

## An SSH public key from a TLS private key

Occasionally useful when the same key pair serves both purposes:

```bash
ssh-keygen -y -f private.key > public.pub
```

> **Reusing a key across purposes is a habit worth avoiding**
>
> It works, but it couples two systems together: rotating the key for one forces a change in the
>   other, and a compromise in either context affects both. Separate keys cost nothing.


## Frequently asked questions

### Can I recover a private key from a public key?

No. That is the entire premise of public key cryptography. If the private key is lost, generate a new one and have the certificate reissued.

### Why do all three sources produce identical output?

Because all three contain the same public key in the same SubjectPublicKeyInfo structure. That is what makes hashing the output a valid equality test across a key, a CSR and a certificate.

### Is it safe to share a public key?

Yes. Public keys are published inside every certificate that any TLS server sends to every client. There is nothing sensitive in one.

### How do I get the public key in DER instead?

Add `-outform der` to the pkey command, or `-pubout` with `-outform der`. See PEM to DER.


## References

- [openssl-pkey(1)](https://docs.openssl.org/3.5/man1/openssl-pkey/) — -pubout for a private key
- [openssl-x509(1)](https://docs.openssl.org/3.5/man1/openssl-x509/) — -pubkey for a certificate

## Related pages

- https://certguru.io/openssl/check-key-matches-certificate/
- https://certguru.io/formats/key/
- https://certguru.io/openssl/view-certificate/
- https://certguru.io/openssl/generate-private-key/
- https://certguru.io/formats/pkcs8/

--
CertGuru — https://certguru.io
