无法使用python3反序列化关键数据

时间:2018-04-18 22:30:16

标签: python-3.x serialization cryptography

我试图将公钥发送到使用python构建的服务器,并且在调用load_pem_public_key时遇到以下错误

  

key =   serialization.load_pem_public_key(pem_data,后端= default_backend())   文件   " /usr/local/lib/python3.5/dist-packages/cryptography/hazmat/primitives/serialization.py" ;,   第24行,在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",   第1040行,在load_pem_public_key中       self._handle_key_loading_error()File" /usr/local/lib/python3.5/dist-packages/cryptography/hazmat/backends/openssl/backend.py",   第1291行,在_handle_key_loading_error中       提出ValueError("无法反序列化关键数据。")ValueError:无法反序列化关键数据。

我的功能就像:

pem_data = request.args.get('public_key').encode()
key = serialization.load_pem_public_key(pem_data,backend=default_backend())

pem_data看起来像(PEM格式)

  

' -----开始公共场合   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'

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这似乎无效。我无法使用openssl加载它。 它包含空格。

详细信息:

$ 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: