0

I have JWE generated in Java using RSA keys. I want to decode this in python. I tried PyJwt as in official documentation. But I am getting this error

Traceback (most recent call last):
  File "/home/bhimasen/Documents/ENV/SysEnv37/lib/python3.7/site-packages/jwt/api_jws.py", line 209, in _load
    payload = base64url_decode(payload_segment)
  File "/home/bhimasen/Documents/ENV/SysEnv37/lib/python3.7/site-packages/jwt/utils.py", line 33, in base64url_decode
    return base64.urlsafe_b64decode(input)
  File "/home/bhimasen/Documents/ENV/SysEnv37/lib/python3.7/base64.py", line 133, in urlsafe_b64decode
    return b64decode(s)
  File "/home/bhimasen/Documents/ENV/SysEnv37/lib/python3.7/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "token_decrypt.py", line 37, in <module>
    decoded = jwt.decode(str_ex, private_key, algorithms=["RS256"])
  File "/home/bhimasen/Documents/ENV/SysEnv37/lib/python3.7/site-packages/jwt/api_jwt.py", line 119, in decode
    decoded = self.decode_complete(jwt, key, algorithms, options, **kwargs)
  File "/home/bhimasen/Documents/ENV/SysEnv37/lib/python3.7/site-packages/jwt/api_jwt.py", line 95, in decode_complete
    **kwargs,
  File "/home/bhimasen/Documents/ENV/SysEnv37/lib/python3.7/site-packages/jwt/api_jws.py", line 149, in decode_complete
    payload, signing_input, header, signature = self._load(jwt)
  File "/home/bhimasen/Documents/ENV/SysEnv37/lib/python3.7/site-packages/jwt/api_jws.py", line 211, in _load
    raise DecodeError("Invalid payload padding") from err
jwt.exceptions.DecodeError: Invalid payload padding

This is the token that I have,

eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiUlNBLU9BRVAtMjU2In0.oprppSvcEKs7gRLRitABobeciYtEOUTQwLli1j8JvCCU_BlbnBurjqqbjLNJR3IZcVDWW1zLfnwJ3vKRSxmuUCOfirmn9_u0UY2dUOMoRO9ubsmQzP2F-Prn7thBocUaeY44vSHBpzOSI78SYbN_jeKAI6Y5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C3yaF9rWwiuBR471yDLEFK-SFPUhgP2rBFkF8qQ01vSI6XFfioAhkF7zHNRu9Ff.5ECWE_6VUAZQ4IIMOjhveA

And algorithm used for encryption is

{"cty":"JWT","enc":"A256GCM","alg":"RSA-OAEP-256"}

can anybody help how to solve this ? or is there any approach that can make this decoding possible ?

jps
  • 20,041
  • 15
  • 75
  • 79
Bhimasen
  • 677
  • 3
  • 8
  • 19
  • 2
    Please post your most recent Python code along with test data so that a repro is possible. – Topaco Dec 15 '21 at 10:37
  • Please also provide the `secret` used to encrypt the token, as we can't debug decoding without it (assuming this token is not 'valuable' if decoded). – match Dec 15 '21 at 10:40
  • pyjwt is not the right tool for this, it can only handle signed tokens (JWS) but not encrypted ones (JWE). But you have a JWE. Try using [Python-JOSE](https://python-jose.readthedocs.io/en/latest/jwe/) – jps Dec 15 '21 at 10:43
  • Does this answer your question? [JWT encrypting payload in python? (JWE)](https://stackoverflow.com/questions/39163000/jwt-encrypting-payload-in-python-jwe) – jps Dec 15 '21 at 10:48
  • and btw. RSA is used for the signature here ("alg") and AES is used for content encyptryption ("enc"). – jps Dec 15 '21 at 10:53
  • @jps Thank you, I didnt know about Jose, I tried with Python-jose. It decrypts the JWE, but after that we need to decode. If I try the decrypted string in online base64decoder, I get the exact result, but if I use python's library base64 it gives, "Incorrect Padding" any idea ? – Bhimasen Dec 15 '21 at 16:16
  • The strings in JWT/JWE are all Base64Url encoded. One difference to normal Base64 is the fact that padding is usually omitted. Even when you use `base64.urlsafe_b64decode` it still complains about padding. You might have to add padding manually like shown [here](https://stackoverflow.com/a/40729471/7329832) (or find a base64url decoder that works correctly) – jps Dec 15 '21 at 16:24

0 Answers0