Skip to content
ecdsa.com
Python cryptographyPython cryptography

Error message

Incorrect password, could not decrypt key

Also printed as

  • ValueError: Incorrect password, could not decrypt key
  • Bad decrypt. Incorrect password?

Wording by release (checked on each): 45.0 and later "Incorrect password, could not decrypt key"; 43–44 report a wrong password as "Could not deserialize key data… the provided password may be incorrect…"; 42 and earlier "Bad decrypt. Incorrect password?".

Python cryptography — ValueError from load_pem_private_key / load_der_private_key / pkcs12.load_key_and_certificates

What it means

The key is encrypted and cryptography tried to decrypt it with the password you supplied, but decryption failed. Encrypted private keys carry no "password hint" — the only signal is that the decrypted bytes are not a valid key — so this always means the bytes you passed are not the passphrase used at encryption time.

Why it happens

How to fix it

  1. 1.

    Read the passphrase byte-exact

    Strip only line endings, never other whitespace, and keep the passphrase as bytes from the source to the loader.

    python
    from pathlib import Path
    
    passphrase = Path("/run/secrets/key_passphrase").read_bytes().rstrip(b"\r\n")
    key = serialization.load_pem_private_key(pem, password=passphrase)
  2. 2.

    Confirm the passphrase outside your code

    If OpenSSL accepts the passphrase typed by hand but your code fails, the difference is in how the code reads it (newline, encoding). If OpenSSL fails too, the passphrase is simply wrong.

    bash
    # Prompts for the passphrase; "bad decrypt" means the passphrase is wrong, not the file
    openssl pkey -in encrypted.pem -noout && echo "passphrase OK"
    printf '%s' "$KEY_PASSPHRASE" | wc -c    # length without a trailing newline

Related errors

Browse the full signature error database