我正在尝试使用“ RS256”对JWT有效负载进行编码,但是我从加密库中收到ValueError: Could not deserialize key data.
错误。
import jwt
private_key = b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM49AgEGBS...'
public_key = b'-----BEGIN PUBLIC KEY-----\nMHYwEAYHKoZIzj0CAQYFK4EEAC...'
encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256')
但是出现以下错误。
Traceback (most recent call last):
File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 205, in prepare_key
key = load_pem_private_key(key, password=None, backend=default_backend())
File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
return backend.load_pem_private_key(data, password)
File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1016, in load_pem_private_key
password,
File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1235, in _load_key
self._handle_key_loading_error()
File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jwt.py", line 65, in encode
json_payload, key, algorithm, headers, json_encoder
File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jws.py", line 113, in encode
key = alg_obj.prepare_key(key)
File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 207, in prepare_key
key = load_pem_public_key(key, backend=default_backend())
File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
return backend.load_pem_public_key(data)
File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1042, in load_pem_public_key
self._handle_key_loading_error()
File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.