Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将.cer文件(我拥有的数字签名证书)添加到pdf中?_Python_Pdf_Digital Signature - Fatal编程技术网

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

Python 如何将.cer文件(我拥有的数字签名证书)添加到pdf中?,python,pdf,digital-signature,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()) E

我想阅读所有的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

导入OpenSSL
cert=OpenSSL.crypto.load\u证书(OpenSSL.crypto.FILETYPE\u ASN1,
打开('digital_sig/tci.cer',encoding='utf-8').read())
错误:
---------------------------------------------------------------------------
UnicodeDecodeError回溯(最近一次呼叫最后一次)
在()
2.
3 cert=OpenSSL.crypto.load_证书(OpenSSL.crypto.FILETYPE_ASN1,
---->4打开('digital_sig/tci.cer',encoding='utf-8')。读取()
解码中的~\Anaconda3\lib\codecs.py(self、input、final)
320#解码输入(考虑缓冲区)
321数据=自缓冲+输入
-->322(结果,消耗)=自身缓冲区解码(数据,自身错误,最终)
323#保留未编码的输入直到下一次呼叫
324 self.buffer=数据[消耗:]
UnicodeDecodeError:“utf-8”编解码器无法解码位置1中的字节0x82:无效的开始字节

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

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