Jaba
Jaba

Reputation: 23

How can I add .cer file (a digital signature certificate that I have) to a pdf?

I want to read all the pdf and add a .cer file to it, digitally sign the pdfs with certificate.Adding or removing "encoding='utf-8'" is not making any difference.

import OpenSSL

cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1,
                                       open('digital_sig/tci.cer', encoding='utf-8').read())

ERROR:
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-7-fc2a32e67543> in <module>()
      2 
      3 cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1,
----> 4                                        open('digital_sig/tci.cer', encoding='utf-8').read())

~\Anaconda3\lib\codecs.py in decode(self, input, final)
    320         # decode input (taking the buffer into account)
    321         data = self.buffer + input
--> 322         (result, consumed) = self._buffer_decode(data, self.errors, final)
    323         # keep undecoded input until the next call
    324         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte

Upvotes: 0

Views: 690

Answers (1)

user6026299
user6026299

Reputation: 101

The certificate is encoded as DER which is a binary encoding. Open the file in binary mode:

open('digital_sig/tci.cer', 'rb').read()

Upvotes: 1

Related Questions