[英]How do I read(open) an ASN.1 file in python
我想使用 python 獲取證書序列號:
der = open('/Users/me/MyApp/Payload/codesign0').read()```
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1, der
cert.get_serial_number()
不幸的是,它在第一行失敗了:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte
如何讀取 Python 中的 ASN.1 文件格式 (DER)?
您將文件作為文本文件打開,這意味着read
嘗試使用 UTF-8 解碼數據以返回str
對象。
相反,將其作為二進制文件打開,以便read
只返回一個bytes
對象,而根本不嘗試解碼數據。
der = open('...', 'rb').read()
你應該試試這個Python-ASN1 encoder and decoder
。 適用於 Python 2.6+ 和 3.3+。 頁面上的簡短示例:
https://pypi.org/project/asn1/
確保在 pip install asn1 之前安裝pip install future
pip install asn1
PyOpenSSL 已被棄用,因此可能需要考慮使用加密模塊
import cryptography
der = open('...', 'rb').read()
cert = cryptography.x509.load_der_x509_certificate(der)
cert.serial_number
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.