如何将.cer文件(我拥有的数字签名证书)添加到pdf中?

时间:2019-02-02 10:03:41

标签: python pdf digital-signature

我想阅读所有pdf文件并向其中添加.cer文件,并用证书对pdf文件进行数字签名。添加或删除“ encoding ='utf-8'”没有任何区别。

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

1 个答案:

答案 0 :(得分:1)

证书被编码为DER,这是一种二进制编码。以二进制模式打开文件:

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