Decrypt gpg file attached from email (file.pgp)

断了今生、忘了曾经 提交于 2019-12-11 02:16:59

问题


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)


来源:https://stackoverflow.com/questions/12900542/decrypt-gpg-file-attached-from-email-file-pgp

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