Decrypting in pgpy fails with “ValueError: Expected: ASCII-armored PGP data”

谁都会走 提交于 2021-01-28 09:57:48

问题


I have an OpenPGP encrypted file and its private key in a text file and know its passphrase.

I tried this below code:

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

How can I decrypt the file in python?


回答1:


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:

Loading Existing Messages

Existing messages can also be loaded very simply. This is nearly identical to loading keys, except that it only returns the new message object, instead of a tuple:

# 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)


来源:https://stackoverflow.com/questions/47525580/decrypting-in-pgpy-fails-with-valueerror-expected-ascii-armored-pgp-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!