pyjwt icon indicating copy to clipboard operation
pyjwt copied to clipboard

Could not deserialize key data when using jwt.encode

Open b99andla opened this issue 5 years ago • 3 comments

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.

b99andla avatar Feb 01 '21 09:02 b99andla

are you facing the same with 2.3.0 release? did you upgraded your code?

auvipy avatar Nov 10 '21 15:11 auvipy

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.

innomatica avatar Dec 13 '21 16:12 innomatica

so we can close this, right?

auvipy avatar Dec 14 '21 03:12 auvipy