问题描述
我在文本文件中有一个OpenPGP加密文件及其私钥,并且知道其密码短语.
I have an OpenPGP encrypted file and its private key in a text file and know its passphrase.
我尝试了以下代码:
import pgpy
emsg = pgpy.PGPMessage.from_file('PGPEcrypted.txt')
key,_ = pgpy.PGPKey.from_file('PrivateKey.txt')
with key.unlock('passcode!'):
print (key.decrypt(emsg).message)
但是在尝试执行时,出现以下错误:
But while trying to execute I am getting following error:
Traceback (most recent call last):
File "D:\Project\PGP\pgp_test.py", line 4, in <module>
key,_ = pgpy.PGPKey.from_file('SyngentaPrivateKey.txt')
File "D:\Anaconda\lib\site-packages\pgpy\types.py", line 191, in from_file
po = obj.parse(data)
File "D:\Anaconda\lib\site-packages\pgpy\pgp.py", line 2252, in parse
unarmored = self.ascii_unarmor(data)
File "D:\Anaconda\lib\site-packages\pgpy\types.py", line 131, in ascii_unarmor
raise ValueError("Expected: ASCII-armored PGP data")
ValueError: Expected: ASCII-armored PGP data
如何在python中解密文件?
How can I decrypt the file in python?
推荐答案
OpenPGP知道两种消息格式:二进制编码(更节省空间)和类似于base64的ASCII装甲(特别是与旧的Internet协议兼容). pgpy.from_file
仅加载ASCII铠装消息.如果您使用的是二进制格式的消息,请改用 pgpy.from_blob
.从 pgpy
文档:
OpenPGP knows two message formats: the binary encoding (more space-efficient) and the base64-like ASCII-armoring (better compatibility especially with old internet protocols). pgpy.from_file
only loads ASCII armored messages. If you've messages in the binary format, use pgpy.from_blob
instead. From the pgpy
documentation:
# PGPMessage will automatically determine if this is a cleartext message or not
message_from_file = pgpy.PGPMessage.from_file("path/to/a/message")
message_from_blob = pgpy.PGPMessage.from_blob(msg_blob)
这篇关于在pgpy中解密失败,并显示"ValueError:预期:ASCII装甲的PGP数据".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!