问题
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