Reputation: 577
I use this code before and it was OK, but after months it does not work. This code suggested by pyjwt documentation without any changes. I am using XUBUNTU and dockerized my project.
import jwt
SEC = "SECRET"
token = jwt.encode(
{'name': ' ', 'admin': True}, SEC, algorithm='RS256'
)
print(token)
raise error
Traceback (most recent call last):
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/jwt/algorithms.py", line 350, in prepare_key
RSAPrivateKey, load_pem_private_key(key_bytes, password=None)
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/cryptography/hazmat/primitives/serialization/base.py", line 25, in load_pem_private_key
return ossl.load_pem_private_key(
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 747, in load_pem_private_key
return self._load_key(
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 929, in _load_key
self._handle_key_loading_error()
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 984, in _handle_key_loading_error
raise ValueError(
ValueError: ('Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).', [<OpenSSLError(code=503841036, lib=60, reason=524556, reason_text=unsupported)>])
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/cc/Documents/test/j.py", line 6, in <module>
token = jwt.encode(
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/jwt/api_jwt.py", line 73, in encode
return api_jws.encode(
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/jwt/api_jws.py", line 160, in encode
key = alg_obj.prepare_key(key)
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/jwt/algorithms.py", line 353, in prepare_key
return cast(RSAPublicKey, load_pem_public_key(key_bytes))
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/cryptography/hazmat/primitives/serialization/base.py", line 35, in load_pem_public_key
return ossl.load_pem_public_key(data)
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 794, in load_pem_public_key
self._handle_key_loading_error()
File "/home/cc/Documents/test/venv/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 984, in _handle_key_loading_error
raise ValueError(
ValueError: ('Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).', [<OpenSSLError(code=75497580, lib=9, reason=108, reason_text=no start line)>])
use this version of library
cffi==1.15.1
cryptography==39.0.0
pycparser==2.21
PyJWT==2.6.0
Upvotes: 0
Views: 824
Reputation: 577
I must follow the correct format for my secret key, Not just a simple string, something just like this (it's an example, found it on the wev)
"""-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu
KUpRKfFLfRYC9AIKjbJTWit+CqvjWYzvQwECAwEAAQJAIJLixBy2qpFoS4DSmoEm
o3qGy0t6z09AIJtH+5OeRV1be+N4cDYJKffGzDa88vQENZiRm0GRq6a+HPGQMd2k
TQIhAKMSvzIBnni7ot/OSie2TmJLY4SwTQAevXysE2RbFDYdAiEBCUEaRQnMnbp7
9mxDXDf6AU0cN/RPBjb9qSHDcWZHGzUCIG2Es59z8ugGrDY+pxLQnwfotadxd+Uy
v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs
/5OiPgoTdSy7bcF9IGpSE8ZgGKzgYQVZeN97YE00
-----END RSA PRIVATE KEY-----"""
Upvotes: 0