Skip to content
ecdsa.com
Python cryptographyPython cryptography

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

How to fix it

  1. 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. 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. 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

Browse the full signature error database