Error message
Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).
Also printed as
- ValueError: Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters). Details: ASN.1 parsing error: short data (needed at least 48 additional bytes)
- Could not deserialize key data. The data may be in an incorrect format, the provided password may be incorrect, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).
- ValueError: ('Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).', [<OpenSSLError(code=503841036, lib=60, reason=524556, reason_text=unsupported)>
Checked across releases: cryptography 45.0 and later append "Details: …" (for example "ASN.1 parsing error: short data"). Versions 43–44 raise the text as a tuple with OpenSSL error objects and add "the provided password may be incorrect" to the sentence (and use it for wrong passwords too); 36–42 raise the tuple form without that phrase.
Python cryptography — ValueError from load_pem_private_key / load_der_private_key / load_der_public_key
What it means
cryptography found key data it could not turn into a key object. For PEM input the armour lines were fine (otherwise you would see "Unable to load PEM file"), so the problem is the DER inside: it is truncated, it is a different structure than the loader expects, or it describes a key cryptography refuses to load — EC keys with explicit curve parameters or keys encrypted with an algorithm it does not support. Recent versions append "Details: …" with the low-level ASN.1 reason.
Why it happens
The base64 body is incomplete or altered
commonA line lost while copying a key from a terminal or a secret manager, a key truncated by a size-limited field, or characters changed by an editor. The header and footer survive, so PEM parsing succeeds and DER parsing fails.
The loader does not match the key structure
commonA public key passed to `load_der_private_key`, a certificate passed to a key loader, or an OpenSSH-format key (`-----BEGIN OPENSSH PRIVATE KEY-----`) passed to `load_pem_private_key` instead of `load_ssh_private_key`.
EC key with explicit curve parameters
occasionalKeys produced by some older tools or HSM exports spell out the curve's prime, coefficients and generator instead of naming the curve by OID. cryptography rejects these on purpose (RFC 5480 requires named curves for PKIX).
Encrypted with an algorithm cryptography does not support
occasionalPKCS#8 files encrypted with legacy PBE schemes (for example PBE-SHA1-RC2 or DES variants) cannot be decrypted by current builds. Re-encrypt them with AES (`openssl pkcs8 -topk8 -v2 aes-256-cbc`).
How to fix it
- 1.
Check the key with OpenSSL first
If `openssl asn1parse` already fails, the file is damaged — get a fresh copy. If OpenSSL loads it and cryptography does not, look at the curve encoding or the encryption algorithm.
bash openssl asn1parse -in key.pem | head -20 # does the DER parse at all? openssl pkey -in key.pem -noout -text | head # private key: type, curve, size openssl pkey -pubin -in pub.pem -noout -text # public key - 2.
Use the loader that matches the key
Choose between PEM, DER and OpenSSH loaders from the data itself, and use the public-key loaders for public keys.
python from cryptography.hazmat.primitives import serialization def load_any_private_key(data: bytes, password: bytes | None = None): if data.lstrip().startswith(b"-----BEGIN OPENSSH PRIVATE KEY-----"): return serialization.load_ssh_private_key(data, password) if data.lstrip().startswith(b"-----BEGIN"): return serialization.load_pem_private_key(data, password) return serialization.load_der_private_key(data, password) - 3.
Convert explicit parameters to a named curve
OpenSSL 3 writes EC keys with named curves by default, so re-exporting the key once fixes explicit-parameter keys for every library.
bash # Re-encode an EC key with a named curve instead of explicit parameters openssl pkey -in explicit.pem -out named.pem openssl ec -in named.pem -param_out -noout -text | grep "ASN1 OID" # e.g. prime256v1
Related errors
Unable to load PEM file. See https://cryptography.io/en/latest/faq/#why-can-t-i…Python cryptographyValid PEM but no BEGIN PUBLIC KEY/END PUBLIC KEY delimiters. Are you sure this…Python cryptographyPassword was given but private key is not encrypted.Python cryptographyerror:0680009B:asn1 encoding routines:ASN1_get_object:too longOpenSSL 3.x CLI (asn1parse, d2i loaders)