Decrypt gpg file attached from email (file.pgp)

2019-07-12 19:39发布

问题:

I'm using email.Message class and gnupg library to:

1 - parse email string fetched from Twisted imap4 client.

2 - get the attached file, a .pgp

3 - decrypt it.

I can decrypt typical mail content, as:

-----BEGIN PGP MESSAGE-----
Version: PGP 9
(...)
-----END PGP MESSAGE-----

but the attachment affair is really making my life a hell.

Well, I tried a lot of different ways, but the most logical should be this:

message = email.message_from_string(content)
if message.is_multipart():
    attachment = message.get_payload(1)
    gpg = gnupg.GPG(gnupghome=settings.PGP_PATH)
    return gpg.decrypt_file(attachment.get_payload(), passphrase=settings.PGP_PASSPH)

The attachment var first lines are:

From nobody Mon Oct 15 18:54:12 2012
Content-type: application/octet-stream; 
 name="No_Norm_AMLT_908_1210201201.txt.pgp"
Content-Disposition: attachment; filename="No_Norm_AMLT_908_1210201201.txt.pgp"
Content-Transfer-Encoding: base64

And then all the encrypted stuff.

Anyway... it's really strange, there's something I don't get. If I download the attachment from a normal mail client software (i.e. Thunderbird) I get a .pgp file that seems binary (strange characters appears if I edit it with a plain text editor) and I can decrypt it using the bash command:

gpg --decrypt No_Norm_AMLT_908_1210201201.txt.pgp

I don't know how to get the same result (the file decrypted) using email.Message class and gnupg, I tried to save the payload of the attachment to a file, and this is different from the downloaded from Thunderbird one, I can't decrypt it, I tried also to put it into a StringIO, and also encoding it with base64.

The message I get from gpg is:

[GNUPG:] NODATA 1
[GNUPG:] NODATA 2
gpg: decrypt_message failed: eof

Thank you!

回答1:

Ok solved! I had to:

base64.decodestring(attachment.get_payload())

then decrypt it using gpg, and worked. This can be figured out because of the header:

Content-Transfer-Encoding: base64

The final code is:

message = email.message_from_string(content)
if message.is_multipart():
    attachment = message.get_payload(1)
    gpg = gnupg.GPG(gnupghome=settings.PGP_PATH)
    return gpg.decrypt_file(base64.decodestring(attachment.get_payload()),
                            passphrase=settings.PGP_PASSPH)