2012-04-28 69 views
5

我正在創建python腳本,它將採用PKCS#12軟件包並打印x509證書中包含的一些信息,並用於此purpouses PyOpenSSL模塊。到目前爲止,我想從證書公鑰獲取。但是PKey對象沒有適當的方法。我可以從哪裏搬出去?任何想法如何獲得公鑰?如何使用PyOpenSSL獲取公鑰?

pfx=open('./1.p12','rb').read() 
PKCS=crypto.load_pkcs12(pfx) 
cert=PKCS.get_certificate() 
PKey=cert.get_pubkey() 

print PKey 
<OpenSSL.crypto.PKey object at 0x012432D8> 

謝謝。

+0

等待,它看起來像你」已經使用'get_pubkey'方法獲取publi c鍵。什麼不工作? – larsks 2012-04-28 11:22:04

+0

那麼,如何打印出來呢? – usp 2012-04-28 12:22:44

回答

-2

這項工作?

print PKey 
<OpenSSL.crypto.PKey object at 0x012432D8> 

from OpenSSL import crypto 

crypto.dump_privatekey(PKey) 
-2

而是使用:

c.dump_privatekey(c.FILETYPE_TEXT,pubkey) 
0

首先,你可以加載證書這樣

from OpenSSL import crypto 

#cert is the encrypted certificate int this format -----BEGIN -----END  
crtObj = crypto.load_certificate(crypto.FILETYPE_PEM, cert) 
pubKeyObject = crtObj.get_pubkey() 
pubKeyString = crypto.dump_publickey(crypto.FILETYPE_PEM,pubKeyObject) 
print pubKeyString 

,你會看到類似

-----BEGIN PUBLIC KEY----- 
.... 
.... 
-----END PUBLIC KEY-----