from cryptography.hazmat.primitives import serialization from cryptography.hazmat.backends import default_backend from json import load import jwt PRIVATE_KEY = "pkcs8privatekey.pem" PAYLOAD = "jwt" with open(PRIVATE_KEY, "rb") as f: pem_bytes = f.read() with open(PAYLOAD, "rb") as f: payload = load(f) print(pem_bytes) print(payload) private_key = serialization.load_pem_private_key(pem_bytes, password=None, backend=default_backend()) encoded = jwt.encode(payload, private_key, algorithm="RS256") print(encoded)