# Convert a PKCS#1 key to PKCS#8

To convert a PKCS#1 private key to PKCS#8, run `openssl pkcs8 -topk8 -in pkcs1.key -out pkcs8.key -nocrypt`. You can tell the two apart from the header line alone — PKCS#1 reads `-----BEGIN RSA PRIVATE KEY-----` and PKCS#8 reads `-----BEGIN PRIVATE KEY-----`. PKCS#8 wraps the same key material in a structure that also records which algorithm the key belongs to, which is why it works for RSA, EC and Ed25519 alike while PKCS#1 is RSA-only. Java, Go and most modern libraries accept only PKCS#8.

Source: https://certguru.io/convert/pkcs1-to-pkcs8/
Last updated: 2026-08-19
Commands verified against OpenSSL 3.5.7.

---

## The command

*PKCS#1 to unencrypted PKCS#8*

```bash
openssl pkcs8 -topk8 -in pkcs1.key -out pkcs8.key -nocrypt
```

| Placeholder | What to put there |
| --- | --- |
| `-topk8` | Convert *to* PKCS#8. Without it the subcommand converts the other way. |
| `-nocrypt` | Write the output unencrypted. Omit to be prompted for a passphrase. |
| `pkcs1.key` | Input starting `-----BEGIN RSA PRIVATE KEY-----`. |
| `pkcs8.key` | Output starting `-----BEGIN PRIVATE KEY-----`. |

## Telling the formats apart

```bash
head -n1 private.key
```

| Header line | Format | Algorithms |
| --- | --- | --- |
| `-----BEGIN RSA PRIVATE KEY-----` | PKCS#1 | RSA only |
| `-----BEGIN EC PRIVATE KEY-----` | SEC1 | EC only |
| `-----BEGIN PRIVATE KEY-----` | PKCS#8, unencrypted | Any |
| `-----BEGIN ENCRYPTED PRIVATE KEY-----` | PKCS#8, encrypted | Any |

> **Why PKCS#8 won**
>
> A PKCS#1 file contains RSA key material and nothing else — the format *is* the algorithm
>   declaration. PKCS#8 wraps the key material in a structure with an explicit
>   `AlgorithmIdentifier`, so one parser handles RSA, EC, Ed25519 and whatever comes next. That is why
>   every modern library standardised on it.

## Producing an encrypted PKCS#8 key

*Encrypt with AES-256 rather than the default*

```bash
openssl pkcs8 -topk8 -in pkcs1.key -out pkcs8.key -v2 aes-256-cbc
```

OpenSSL prompts for the passphrase. In a script, add `-passout file:/run/secrets/key-password`
rather than putting the secret on the command line.

## Converting an EC key

The same command works — `-topk8` is not RSA-specific despite the format it usually replaces:

```bash
openssl pkcs8 -topk8 -in ec-sec1.key -out ec-pkcs8.key -nocrypt
```

## Verify the conversion

The key must still match its certificate afterwards. Comparing public keys proves the material
survived intact:

*These two must print the same hash*

```bash
openssl pkey -in pkcs1.key -pubout | openssl sha256
openssl pkey -in pkcs8.key -pubout | openssl sha256
```

And confirm the header is what you expected:

```bash
head -n1 pkcs8.key
```

Expected output:

```
-----BEGIN PRIVATE KEY-----
```

If it reads `BEGIN ENCRYPTED PRIVATE KEY`, you left out `-nocrypt` and a server that cannot prompt
for a passphrase will fail to start.

## Going the other way

See [convert PKCS#8 to PKCS#1](/convert/pkcs8-to-pkcs1/).


## Frequently asked questions

### How do I tell PKCS#1 from PKCS#8 at a glance?

The header line. `-----BEGIN RSA PRIVATE KEY-----` is PKCS#1, `-----BEGIN PRIVATE KEY-----` is unencrypted PKCS#8, and `-----BEGIN ENCRYPTED PRIVATE KEY-----` is PKCS#8 with a passphrase. `-----BEGIN EC PRIVATE KEY-----` is the EC equivalent of PKCS#1, defined by SEC1.

### Does this change the key?

No. The same private key material is rewrapped in a different ASN.1 structure. The public key, the certificate it matches and every signature it has produced remain valid.

### Why does Java reject my key?

Java's KeyFactory reads PKCS#8 only. A `BEGIN RSA PRIVATE KEY` file will fail to parse no matter how it is loaded, which is the single most common reason to run this conversion.

### What if I want the output encrypted?

Drop `-nocrypt` and OpenSSL prompts for a passphrase, producing a `BEGIN ENCRYPTED PRIVATE KEY` file. Add `-v2 aes-256-cbc` to choose the cipher explicitly rather than accepting the default.


## References

- [openssl-pkcs8(1)](https://docs.openssl.org/3.5/man1/openssl-pkcs8/) — -topk8
- [RFC 5958: asymmetric key packages](https://www.rfc-editor.org/rfc/rfc5958) — PKCS#8, the self-describing private key container

## Related pages

- https://certguru.io/convert/pkcs8-to-pkcs1/
- https://certguru.io/formats/pkcs8/
- https://certguru.io/formats/key/
- https://certguru.io/openssl/generate-private-key/
- https://certguru.io/openssl/remove-passphrase-from-key/

--
CertGuru — https://certguru.io
