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DAWBgNVBAsMDzAwRDBDMDAwMDAwOGV6UDEXMBUGA1UECgwO\nU2FsZXNmb3JjZS5jb20xFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28xCzAJBgNVBAgM\nAkNBMQwwCgYDVQQGEwNVU0GCDgF3Q/ujpwAAAAAnAcLlMA0GCSqGSIb3DQEBCwUA\nA4IBAQDPkaJgi7mqMmhwz7x7q7LssZiuuenYMzFWewry6SZmjqZwLWvxhSWLmog9\nkeNQH7nAbI+6KrMHJ36LPJdD+zbNYQaz8S9qW8HBVOap/J6wKYOC6h/dtKLcfmbt\nZlCyCy6SJv0mDQyxMVyq6gybjRrBnNcoy/hTAEQNhmHL6aZ7E2/eX/EOD1Y1EHWl\nmFqmtpST36F9Y2WaYpKVwBPcZqcwxPpGrJ95FfxR+kkA84Ke3SZ/RHHkZzexcOOR\n5YLaIZf9sVzslSsLrt6uaFMQOdTT5kY1CvCMHaw3j/kDrIpladwyfzhmVgUVx1D3\nxF5JnmOe6qhhyPwRMg7Myw7KIfF5\n-----END CERTIFICATE-----'
encoded = <my jwt token>
import jwt
decoded = jwt.decode(encoded, public_key, algorithms=["RS256"])
这引发了以下错误:
ValueError: Could not deserialize key data.
所以我尝试以不同的方式加载公钥:
from Crypto.PublicKey import RSA
pub_key = RSA.importKey(public_key)
这提出了:
ValueError: RSA key format is not supported
同样,当我尝试将其加载到二进制文件(b'----- ...)时,也会引发相同的错误
更新:
设法使用 pycryptodome 创建 RSA 对象:
from Crypto.PublicKey.RSA import import_key
key = import_key(public_key)
但是在运行时:
decoded = jwt.decode(encoded, key, algorithms=["RS256"])
我越来越:
TypeError: Expecting a PEM-formatted key.