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
A trailing newline is part of the password
commonPassphrases stored with `echo secret > file`, in Kubernetes secrets created from files, or in `.env` files often end with `\n`. Reading the file verbatim passes one byte too many.
The passphrase belongs to another key
commonSeveral keys and several passphrases in one secret store, or a key re-encrypted during rotation while the application kept the old passphrase.
Non-ASCII passphrase encoded differently
occasionalThe tool that encrypted the key may have used Latin-1 or UTF-16 for a passphrase with non-ASCII characters while your code encodes UTF-8. The characters match; the bytes do not.
PKCS#12 files from older tools
occasionalSome legacy `.p12` exports use an empty password differently (no password vs empty string). Try `None` and `b""` deliberately rather than guessing other values.
How to fix it
- 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.
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