Could not deserialize key data when using jwt.encode
I have a solution that has been working for at least a year, but since updating to pyjwt 2.0.1 does not work.
return jwt.encode(
payload,
get_private_key(),
algorithm="RS256"
).decode("utf-8")
get_private_key() returns (removed data to not show secrets...):
b'-----BEGIN RSA PRIVATE KEY-----\\nMII\\n-----END RSA PRIVATE KEY-----'
Expected Result
I expect a correctly encoded JWT to be generated.
Actual Result
load_pem_private_key in jwt algorithms returns a valueerror.
Could not deserialize key data. The data may be in an incorrect format or it may be encrypted with an unsupported algorithm.
System Information
$ python -m jwt.help
{
"cryptography": {
"version": "3.3.1"
},
"implementation": {
"name": "CPython",
"version": "3.8.5"
},
"platform": {
"release": "4.19.121-linuxkit",
"system": "Linux"
},
"pyjwt": {
"version": "2.0.1"
}
}
This command is only available on PyJWT v1.6.3 and greater. Otherwise, please provide some basic information about your system.
are you facing the same with 2.3.0 release? did you upgraded your code?
I had the same issue with v2.3.0. It turned out that my RSA key pair was not generated properly. You need to use OpenSSL:
openssl genrsa -out jwt-key 4096
openssl rsa -in jwt-key -poutput > jwt-key.pub
as described here.
so we can close this, right?