PKCS#8, SPKI and PKCS#1: three shapes of key file
Key files look numerous, but the envelopes are few. A private key is PKCS#8, PKCS#1 or SEC1 ECPrivateKey; a public key is almost always SubjectPublicKeyInfo (SPKI). The PEM label and the first tag tell you which.
| PEM label | Structure | First child |
|---|---|---|
| PRIVATE KEY | PKCS#8 PrivateKeyInfo | INTEGER 0 |
| ENCRYPTED PRIVATE KEY | PKCS#8 EncryptedPrivateKeyInfo | SEQUENCE (AlgorithmIdentifier) |
| RSA PRIVATE KEY | PKCS#1 RSAPrivateKey | INTEGER 0 then a large INTEGER |
| EC PRIVATE KEY | SEC1 ECPrivateKey | INTEGER 1 |
| PUBLIC KEY | SubjectPublicKeyInfo | SEQUENCE (AlgorithmIdentifier) |
PKCS#8: a private key with its algorithm
PrivateKeyInfo ::= SEQUENCE { version INTEGER, privateKeyAlgorithm AlgorithmIdentifier, privateKey OCTET STRING, attributes [0] OPTIONAL }
The privateKey OCTET STRING contains the actual key one level deeper: PKCS#1 RSAPrivateKey for RSA, ECPrivateKey for EC. Decode a PKCS#8 key and you will see a second SEQUENCE begin inside the OCTET STRING.
- version is normally 0. Version 1 appears in variants that also carry a public key.
- privateKeyAlgorithm is an OID such as rsaEncryption (1.2.840.113549.1.1.1), id-ecPublicKey (1.2.840.10045.2.1) or id-Ed25519 (1.3.101.112).
- For EC, the AlgorithmIdentifier parameters hold the curve OID: prime256v1 (1.2.840.10045.3.1.7), secp384r1 (1.3.132.0.34) and so on.
SPKI: the standard public key envelope
SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING }
For an RSA public key the BIT STRING holds SEQUENCE { modulus INTEGER, publicExponent INTEGER }. For EC it holds the uncompressed point starting with 0x04 (04 ‖ X ‖ Y). That is why opening the BIT STRING shows a SEQUENCE for RSA and what looks like random bytes for EC.
- A leading 0x00 in the RSA modulus is sign padding. The real key size is the length without it.
- An EC point is 65, 97 or 133 bytes depending on the curve (1 + twice the coordinate size).
- An Ed25519 public key is 32 raw bytes directly inside the BIT STRING.
Encrypted PKCS#8
ENCRYPTED PRIVATE KEY is EncryptedPrivateKeyInfo: SEQUENCE { encryptionAlgorithm AlgorithmIdentifier, encryptedData OCTET STRING }. The algorithm holds PBES2 (1.2.840.113549.1.5.13) with PBKDF2 (1.2.840.113549.1.5.12) parameters, and encryptedData is ciphertext. The inner key structure is unreadable until it is decrypted with the password.
Common confusions
- A block labelled PRIVATE KEY whose first tag is INTEGER 1 may be an EC (SEC1) key. Labels are sometimes wrong.
- People assume fields are missing because they never opened the PKCS#8 OCTET STRING. Go one level deeper.
- The nine INTEGERs of a PKCS#1 RSA key are version, n, e, d, p, q, dp, dq, qinv. Do not swap n and e.
- If a public key file contains a private exponent, it is not a public key.
Real private keys should not be pasted into a browser at all. Study structure with the synthetic samples, and handle production keys offline.
Questions
What is the difference between PKCS#8 and PKCS#1?
PKCS#1 is an RSA-only structure; PKCS#8 is a general envelope that names the algorithm. The usual form places PKCS#1 inside PKCS#8, and EC or Ed25519 keys use the same envelope.
Can a private key be derived from a public key?
For RSA the private exponent is mathematically derivable from modulus and public exponent, but not at real key sizes. For EC the private scalar cannot be computed from the public point.
What if the SPKI BIT STRING is empty?
The structure is invalid or truncated. A BIT STRING needs at least the unused-bit count byte plus key bytes.
Load the synthetic PKCS#8 and SPKI samples and compare the two structures side by side.
Open the toolThis guide follows ITU-T X.690 (DER/BER), RFC 5280 (X.509), RFC 5208 (PKCS#8), RFC 5480 (EC public keys), RFC 5915 (EC private keys) and RFC 3279 (ECDSA signatures). Every example is synthetic.