Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jwt - Python, load RSA-256 public key into code

I'm trying to load a public key in RSA-256 in Python.

public_key=Out[24]: '-----BEGIN CERTIFICATE-----
MIIEYTCCA0mgAwIBAgIOAXdD+6OnAAAAACcBwuUwDQYJKoZIhvcNAQELBQAweTER
MA8GA1UEAwwIQWxkZXJhYW4xGDAWBgNVBAsMDzAwRDBDMDAwMDAwOGV6UDEXMBUG
A1UECgwOU2FsZXNmb3JjZS5jb20xFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28xCzAJ
BgNVBAgMAkNBMQwwCgYDVQQGEwNVU0EwHhcNMjEwMTI3MTMxNTAwWhcNMjIwMTI3
MTIwMDAwWjB5MREwDwYDVQQDDAhBbGRlcmFhbjEYMBYGA1UECwwPMDBEMEMwMDAw
MDA4ZXpQMRcwFQYDVQQKDA5TYWxlc2ZvcmNlLmNvbTEWMBQGA1UEBwwNU2FuIEZy
YW5jaXNjbzELMAkGA1UECAwCQ0ExDDAKBgNVBAYTA1VTQTCCASIwDQYJKoZIhvcN
AQEBBQADggEPADCCAQoCggEBAN0KKolUflB1/wZDnFScGkFf1SkoeuOFPw+UURCz
c8tPLOLPLIt/qkUBt5X8ptCVuWfIoscrj4my25xW9c21d4cxTM7wlUDunoH2lohu
4KbrpOoGJ0HelsPV9BV3RKuj/hDsypS5StW1RLUZYBOUFRQaQPB7drb1KUYqUV1q
Xbf/gWm9TGq5vQS29LZYDgW+T+6xBGltqNtlNAPRUbShQUsmTizZM8v54zbtSjaP
WHdZ4S1VWqSt+03JRobEIMBHr2L2otsAXOBfaBUqVkGDi8b1YynBOa0A+6cogq9x
b3xH6fW+RTvTyA9VkptcFfWO+ePOTqkR+f8ObhO1if94c50CAwEAAaOB5jCB4zAd
BgNVHQ4EFgQUMj3VA9/H7zXx8UsOCAuQuRQGLkswDwYDVR0TAQH/BAUwAwEB/zCB
sAYDVR0jBIGoMIGlgBQyPdUD38fvNfHxSw4IC5C5FAYuS6F9pHsweTERMA8GA1UE
AwwIQWxkZXJhYW4xGDAWBgNVBAsMDzAwRDBDMDAwMDAwOGV6UDEXMBUGA1UECgwO
U2FsZXNmb3JjZS5jb20xFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28xCzAJBgNVBAgM
AkNBMQwwCgYDVQQGEwNVU0GCDgF3Q/ujpwAAAAAnAcLlMA0GCSqGSIb3DQEBCwUA
A4IBAQDPkaJgi7mqMmhwz7x7q7LssZiuuenYMzFWewry6SZmjqZwLWvxhSWLmog9
keNQH7nAbI+6KrMHJ36LPJdD+zbNYQaz8S9qW8HBVOap/J6wKYOC6h/dtKLcfmbt
ZlCyCy6SJv0mDQyxMVyq6gybjRrBnNcoy/hTAEQNhmHL6aZ7E2/eX/EOD1Y1EHWl
mFqmtpST36F9Y2WaYpKVwBPcZqcwxPpGrJ95FfxR+kkA84Ke3SZ/RHHkZzexcOOR
5YLaIZf9sVzslSsLrt6uaFMQOdTT5kY1CvCMHaw3j/kDrIpladwyfzhmVgUVx1D3
xF5JnmOe6qhhyPwRMg7Myw7KIfF5
-----END CERTIFICATE-----'

encoded = <my jwt token>
import jwt
decoded = jwt.decode(encoded, public_key, algorithms=["RS256"])

This raised the following error:

ValueError: Could not deserialize key data.

So I tried to load the public key in a different way:

from Crypto.PublicKey import RSA
pub_key = RSA.importKey(public_key)

which raised:

ValueError: RSA key format is not supported

also when I try it with loading it to binary (b'----- ...), same error is raised

Update:

managed to create RSA object with pycryptodome:

from Crypto.PublicKey.RSA import import_key
key = import_key(public_key)

but then when running:

decoded = jwt.decode(encoded, key, algorithms=["RS256"])

I'm getting:

TypeError: Expecting a PEM-formatted key.
question from:https://stackoverflow.com/questions/65923429/python-load-rsa-256-public-key-into-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...