I'm trying to send a public key to a server built with python and i'm getting the following error while calling load_pem_public_key
key = serialization.load_pem_public_key(pem_data,backend=default_backend())
File "/usr/local/lib/python3.5/dist-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
return backend.load_pem_public_key(data)
File "/usr/local/lib/python3.5/dist-packages/cryptography/hazmat/backends/openssl/backend.py", line 1040, in load_pem_public_key
self._handle_key_loading_error()
File "/usr/local/lib/python3.5/dist-packages/cryptography/hazmat/backends/openssl/backend.py", line 1291, in _handle_key_loading_error
raise ValueError("Could not deserialize key data.") ValueError: Could not deserialize key data.
and my function is like :
pem_data = request.args.get('public_key').encode()
key = serialization.load_pem_public_key(pem_data,backend=default_backend())
pem_data looks
like (PEM format)
-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxt/G jhPU1Pt 0q68jEu\n9aXUohWo46G38t6KV1WQBbGpn28Uz9JvthITARtSW Q/6Sc3MNQdjDyUrdL64Mmq\nPTWVVOS8wSB ZayiZq48isoPQh4TPtv3exFNMaI2/339laoVX3aFjF7zxcyKgTVk\ng4Dm2ny9iPIEN3BTGn3FxD9lz3LyxVUd2pAV29wdI06TdDs8xI1P1xp8/gDJxZH3\nUroRAGphXTnfxn Aq5lAMesGG6HnO0/e KBLGeKFEIkIaH31555yQ3uBptl 3c9m\nkTOeBzMlvxX0rAVsZAXC8k1xoD28glUXQvcewq DqmsUNAVOLfHFhaNP6Lztjjuz\nMQIDAQAB\n-----END PUBLIC KEY-----\n
Thank you for your help !
This seems to be invalid. I can't load it using openssl
.
It contains spaces.
Details:
$ cat pubkey.pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxt/G jhPU1Pt 0q68jEu
9aXUohWo46G38t6KV1WQBbGpn28Uz9JvthITARtSW Q/6Sc3MNQdjDyUrdL64Mmq
PTWVVOS8wSB ZayiZq48isoPQh4TPtv3exFNMaI2/339laoVX3aFjF7zxcyKgTVk
g4Dm2ny9iPIEN3BTGn3FxD9lz3LyxVUd2pAV29wdI06TdDs8xI1P1xp8/gDJxZH3
UroRAGphXTnfxn Aq5lAMesGG6HnO0/e KBLGeKFEIkIaH31555yQ3uBptl 3c9m
kTOeBzMlvxX0rAVsZAXC8k1xoD28glUXQvcewq DqmsUNAVOLfHFhaNP6Lztjjuz
MQIDAQAB
-----END PUBLIC KEY-----
$ openssl pkey -inform PEM -pubin -in pubkey.pem -noout
unable to load Public Key
140571453707392:error:0D07209B:asn1 encoding routines:ASN1_get_object:too long:../crypto/asn1/asn1_lib.c:91:
140571453707392:error:0D068066:asn1 encoding routines:asn1_check_tlen:bad object header:../crypto/asn1/tasn_dec.c:1118:
140571453707392:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:290:Type=X509_PUBKEY
140571453707392:error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib:../crypto/pem/pem_oth.c:33:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With