2012-04-28 66 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-----