I want to get a certificates serial number using python:
JavaScript
x
4
1
der = open('/Users/me/MyApp/Payload/codesign0').read()```
2
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1, der
3
cert.get_serial_number()
4
Unfortunately it fails in the first line:
JavaScript
1
2
1
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte
2
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.
JavaScript
1
2
1
der = open('...', 'rb').read()
2