I want to get a certificates serial number using python:
der = open('/Users/me/MyApp/Payload/codesign0').read()``` cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1, der cert.get_serial_number()
Unfortunately it fails in the first line:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte
How do I read an ASN.1 file format (DER) in Python?
Advertisement
Answer
You are opening the file as a text file, which means read
tries to decode the data using UTF-8 in order to return a str
object.
Instead, open it as a binary file, so that read
simply returns a bytes
object without trying to decode the data at all.
der = open('...', 'rb').read()