Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以位为单位提取公钥长度_Python_M2crypto - Fatal编程技术网

Python 以位为单位提取公钥长度

Python 以位为单位提取公钥长度,python,m2crypto,Python,M2crypto,我的目标是开发一个Python脚本来连接到主机,并确定服务器公钥长度(以位为单位),类似于运行openssl: (openssl s_client -connect 10.18.254.29:443) yada yada yada Server certificate -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- Server public key is 2048 bit 我已开始编写以下基本脚本: from M2Crypto im

我的目标是开发一个Python脚本来连接到主机,并确定服务器公钥长度(以位为单位),类似于运行openssl:

(openssl s_client -connect 10.18.254.29:443)
yada yada yada
Server certificate
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
Server public key is 2048 bit
我已开始编写以下基本脚本:

from M2Crypto import SSL, RSA
SSL.Connection.clientPostConnectionCheck = None
ctx = SSL.Context()
conn = SSL.Connection(ctx)
conn.connect(('1.1.1.1', 443))
cert = conn.get_peer_cert()
print cert.get_issuer().as_text()

print cert.get_subject().as_text()
print cert.get_fingerprint()

print cert.get_pubkey().get_rsa().as_pem()
我似乎找不到显示键的长度属性的方法。有什么想法吗?

通常的方法是:

print key.size()
至于PKey,这是以字节为单位的大小,而不是位。所以,如果你想得到“2048”,你需要乘以8

如果您知道它是RSA,并且已经调用了
get\u RSA()
,则可以执行以下操作:

print len(rsa)
不要说它是做什么的,但它会以位的形式返回长度

与M2Crypto一样,您真正需要查看的是libssl/libcrypto文档(而不是openssl命令行工具)。而且,如果您猜不出调用了哪些C函数,那么源代码通常非常简单

例如,您可以看到:

是:

def\uuuu len\uuuu(自):

返回m2.rsa_size(self.rsa)基于@abarnert的帮助,我将代码更改为

from M2Crypto import SSL, RSA

SSL.Connection.clientPostConnectionCheck = None
ctx = SSL.Context()
conn = SSL.Connection(ctx)
conn.connect(('10.18.254.29', 443))
cert = conn.get_peer_cert()

print cert.get_issuer().as_text()

print cert.get_subject().as_text()
print cert.get_fingerprint()

**def size(self):
    """
    Return the size of the key in bytes.
    """
    return m2.pkey_size(self.pkey)**
打印证书get_pubkey().size()*8

def __len__(self):
    return m2.rsa_size(self.rsa) << 3
from M2Crypto import SSL, RSA

SSL.Connection.clientPostConnectionCheck = None
ctx = SSL.Context()
conn = SSL.Connection(ctx)
conn.connect(('10.18.254.29', 443))
cert = conn.get_peer_cert()

print cert.get_issuer().as_text()

print cert.get_subject().as_text()
print cert.get_fingerprint()

**def size(self):
    """
    Return the size of the key in bytes.
    """
    return m2.pkey_size(self.pkey)**