Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.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
Android 使用m2crypto在apk中打印证书_Android_Python_Digital Certificate_M2crypto - Fatal编程技术网

Android 使用m2crypto在apk中打印证书

Android 使用m2crypto在apk中打印证书,android,python,digital-certificate,m2crypto,Android,Python,Digital Certificate,M2crypto,我想像openssl命令那样显示android APK证书信息。像 $ openssl pkcs7 -inform DER -in CERT.RSA -print_certs subject=... -----BEGIN CERTIFICATE----- MIIEBzCCAu+gAwIBAgIEKkPNCjANBgkqhkiG9w0BAQsFADCBsjEPMA0GA1UEBhMG ... 5be3OOWPt+mHkeWxsei9r+S7tuWkp+WOpjEVMBMGA1UECgwM6YeR5

我想像
openssl
命令那样显示android APK证书信息。像

$ openssl pkcs7 -inform DER -in CERT.RSA -print_certs
subject=...
-----BEGIN CERTIFICATE-----
MIIEBzCCAu+gAwIBAgIEKkPNCjANBgkqhkiG9w0BAQsFADCBsjEPMA0GA1UEBhMG
...
5be3OOWPt+mHkeWxsei9r+S7tuWkp+WOpjEVMBMGA1UECgwM6YeR5bGx572R57uc
-----END CERTIFICATE-----
起初我使用了
PyCrypto
,但我发现它不包含X509格式。尝试
M2Crypto
后,它将输出如下错误

In [7]: X509.load_cert('CERT.RSA', X509.FORMAT_DER)
---------------------------------------------------------------------------
X509Error                                 Traceback (most recent call last)
<ipython-input-7-821a670a1ab6> in <module>()
----> 1 X509.load_cert('CERT.RSA', X509.FORMAT_DER)

/usr/local/lib/python2.7/dist-packages/M2Crypto/X509.pyc in load_cert(file, format)
    613         cptr = m2.d2i_x509(bio._ptr())
    614         if cptr is None:
--> 615             raise X509Error(Err.get_error())
    616         return X509(cptr, _pyfree=1)
    617     else:

X509Error: 140335753901888:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag:tasn_dec.c:1337:
140335753901888:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:tasn_dec.c:388:Type=X509_CINF
140335753901888:error:0D08303A:asn1 encoding routines:ASN1_TEMPLATE_NOEXP_D2I:nested asn1 error:tasn_dec.c:769:Field=cert_info, Type=X509
[7]中的
:X509.load\u cert('cert.RSA',X509.FORMAT\u DER)
---------------------------------------------------------------------------
X509错误回溯(最近一次呼叫上次)
在()
---->1 X509.加载证书('cert.RSA',X509.FORMAT\u DER)
/加载证书中的usr/local/lib/python2.7/dist-packages/M2Crypto/X509.pyc(文件,格式)
613 cptr=m2.d2i_x509(bio._ptr())
614如果cptr为无:
-->615 raise X509Error(Err.get_error())
616返回X509(cptr,自由=1)
617其他:
X509错误:140335753901888:错误:0D0680A8:asn1编码例程:asn1\u检查\u TLEN:错误标记:tasn\u dec.c:1337:
140335753901888:错误:0D07803A:asn1编码例程:asn1_项_EX_D2I:嵌套asn1错误:tasn_dec.c:388:Type=X509_CINF
140335753901888:错误:0D08303A:asn1编码例程:asn1\U模板\U NOEXP\U D2I:嵌套asn1错误:tasn\U dec.c:769:字段=证书信息,类型=X509
显示base64编码证书的正确方法是什么?

灵感来源于我使用
pyasn1
pyasn1\u模块
通过以格式提取文件来显示base64编码证书

下面是一个简单的脚本

from pyasn1.codec.der import decoder, encoder
from pyasn1_modules import rfc2315

contentInfo, _ = decoder.decode(open('CERT.RSA', 'rb').read(), asn1Spec=rfc2315.ContentInfo())
content = contentInfo.getComponentByName('content')
signedData, _ = decoder.decode(content, asn1Spec=rfc2315.SignedData())
certs = signedData.getComponentByName('certificates')
cert = certs.getComponentByPosition(0)
print encoder.encode(cert).encode('base64')
受此启发,我使用
pyasn1
pyasn1\u模块
通过以格式提取文件来显示base64编码的证书

下面是一个简单的脚本

from pyasn1.codec.der import decoder, encoder
from pyasn1_modules import rfc2315

contentInfo, _ = decoder.decode(open('CERT.RSA', 'rb').read(), asn1Spec=rfc2315.ContentInfo())
content = contentInfo.getComponentByName('content')
signedData, _ = decoder.decode(content, asn1Spec=rfc2315.SignedData())
certs = signedData.getComponentByName('certificates')
cert = certs.getComponentByPosition(0)
print encoder.encode(cert).encode('base64')