ecdsa.com
Python cryptographyPython cryptography

Error message

Password was not given but private key is encrypted

The mirror case exists too: "Password was given but private key is not encrypted." — same call, opposite mismatch (both reproduced on cryptography 50.0).

Python cryptography — TypeError from load_pem_private_key

What it means

You called load_pem_private_key(data, password=None) on a key that is encrypted — its PEM says ENCRYPTED PRIVATE KEY, or carries Proc-Type: 4,ENCRYPTED headers. The library can see the encryption envelope and refuses to guess: it needs the passphrase as bytes, or a key that is genuinely unencrypted.

Why it happens

How to fix it

  1. 1.

    Supply the passphrase as bytes

    Pass the password through your secret store, encoded to bytes; strip whitespace defensively.

    python
    import os
    from cryptography.hazmat.primitives.serialization import load_pem_private_key
    
    password = os.environ["KEY_PASSPHRASE"].strip().encode()
    key = load_pem_private_key(pem_bytes, password=password)
  2. 2.

    Or decrypt the key once, at provisioning time

    If the runtime environment cannot manage a passphrase, store the key unencrypted in a proper secret manager — an encrypted key whose password sits in the same env file adds no protection. And if the passphrase ever traveled next to the key in a repository, rotate the key: at that point the encryption was decoration.

    bash
    openssl pkey -in encrypted.pem -out plain.pem
    # (prompts once for the passphrase; protect plain.pem via the secret store)

Related errors

← Browse the full signature error database