【问题标题】:Decrypting in pgpy fails with "ValueError: Expected: ASCII-armored PGP data"在 pgpy 中解密失败,并出现“ValueError: Expected: ASCII-armored PGP data”
【发布时间】:2017-11-28 06:53:22
【问题描述】:

我在文本文件中有一个 OpenPGP 加密文件及其私钥,并且知道它的密码。

我尝试了以下代码:

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)

但在尝试执行时出现以下错误:

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 中解密文件?

【问题讨论】:

  • 导出私钥时是否使用了-a标志?

标签: python encryption gpgpu pgp openpgp


【解决方案1】:

OpenPGP 知道两种消息格式:二进制编码(更节省空间)和类似 base64 的 ASCII 铠装(更好的兼容性,尤其是与旧互联网协议的兼容性)。 pgpy.from_file 仅加载 ASCII 装甲消息。如果您有二进制格式的消息,请改用pgpy.from_blob。来自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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2019-06-11
    • 2022-10-31
    • 2020-09-22
    • 2021-02-07
    相关资源
    最近更新 更多