我試圖加載使用cryptography模塊的公共密鑰加載RSA公共密鑰,這是關鍵的樣子:如何使用Python的加密模塊
>>> print(pubkey)
-----BEGIN RSA PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+6gvHdCUCjnc4hSMwbdIIspk4
69pVAzjjb8tDJsCH/QpiK9vXe4nDZ7p9kiw2ACw0fkWaPnApKBwXNB9Nd9Sf+XFt
cIzdqKKBcAqZZCu2pA729amNRug9DoZdkstaBG+VfTxXhdzQRSTxxqJQWgdV8ejK
kt4D1M6pAiTkAyD0eQIDAQAB
-----END RSA PUBLIC KEY-----
我試圖加載它使用load_pem_public_key()
方法:
>>> from cryptography.hazmat.backends import default_backend
>>> from cryptography.hazmat.primitives.serialization import load_pem_public_key
>>> load_pem_public_key(pubkey, default_backend())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/elias/.virtualenvs/ckpypkg/local/lib/python2.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
return backend.load_pem_public_key(data)
File "/home/elias/.virtualenvs/ckpypkg/local/lib/python2.7/site-packages/cryptography/hazmat/backends/multibackend.py", line 285, in load_pem_public_key
return b.load_pem_public_key(data)
File "/home/elias/.virtualenvs/ckpypkg/local/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1376, in load_pem_public_key
self._handle_key_loading_error()
File "/home/elias/.virtualenvs/ckpypkg/local/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1595, in _handle_key_loading_error
raise ValueError("Could not unserialize key data.")
ValueError: Could not unserialize key data.
我做錯了什麼?這個鍵有什麼問題嗎?爲什麼它不能被反序列化?
OpenSSL的版本:
$ openssl version
OpenSSL 1.0.1f 6 Jan 2014
更新:我只是測試相同的代碼使用不同的密鑰(同一個從this other SO question)和它的工作,這使得這個還更令人費解:爲什麼它適用於那個鑰匙,而不是我的?
恩,你確定嗎?因爲那太奇怪了!你能指出任何確認這種行爲的來源嗎? – elias
這不能是原因,我剛剛測試了另一個關鍵(從[這個問題](http://stackoverflow.com/questions/10569189/how-to-read-a-rsa-public-key-in- peg-pkcs1-format) – elias
----- BEGIN RSA PUBLIC KEY -----用於PKCS#1標準,----- BEGIN PUBLIC KEY -----用於PKCS# 8標準,我對您的公鑰進行了解碼,並且它的der序列符合PKCS#8的序列。您可以在這裏找到更多關於DER序列與PKCS#1和PKCS#8不同的信息:https://tls.mbed.org/kb/cryptography/asn1-key-structures-in-der-and-pem。順便說一句,我在發佈之前測試了我的答案,並且它工作正常,你應該至少在放棄之前嘗試。 – godvsdeity