使用Python进行PGP签名的多部分电子邮件 [英] PGP-signing multipart e-mails with Python

查看:179
本文介绍了使用Python进行PGP签名的多部分电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试向我的小型电子邮件发送脚本添加PGP签名支持(使用Python 3.x和 python-gnupg 模块)。 p>

签名消息的代码是:

  gpg = gnupg.GPG( )
basetext = basemsg.as_string()。replace('\n','\r\n')
签名= str(gpg.sign(basetext,detach = True))
如果签名:
signmsg = messageFromSignature(signature)
msg = MIMEMultipart(_subtype = signed,micalg = pgp-sha1,
protocol = application / pgp-signature )
msg.attach(basemsg)
msg.attach(signmsg)
否则:
print('警告:邮件签名失败!')

(此处 basemsg email.message.Message 类型。)



messageFromSignature 函数是:

  def messageFromSignature(signature):
message = Message()
message ['Content-Type'] ='application / pgp-signature; name = signature.asc'
message ['Content-Description'] ='OpenPGP数字签名'
message.set_payload(signature)
返回消息

然后我将所有所需的标头添加到消息( msg )并发送



这对于非多部分邮件效果很好,但是当 basemsg 是多部分邮件( multipart / alternative multipart / mixed )。



手动验证签名相应的文本有效,但是Evolution和Mutt报告签名无效。



有人可以指出我的错误吗?

解决方案

问题是Python的 email.generator 模块没有在签名部分之前添加换行符。我已经报告说上游为 http://bugs.python.org/issue14983



(该错误已在2014年的Python2.7和3.3+中修复)


I'm currently trying to add PGP signing support to my small e-mail sending script (which uses Python 3.x and python-gnupg module).

The code that signs message is:

gpg = gnupg.GPG()
basetext = basemsg.as_string().replace('\n', '\r\n')
signature = str(gpg.sign(basetext, detach=True))
if signature:
    signmsg = messageFromSignature(signature)
    msg = MIMEMultipart(_subtype="signed", micalg="pgp-sha1",
    protocol="application/pgp-signature")
    msg.attach(basemsg)
    msg.attach(signmsg)
else:
    print('Warning: failed to sign the message!')

(Here basemsg is of email.message.Message type.)

And messageFromSignature function is:

def messageFromSignature(signature):
    message = Message()
    message['Content-Type'] = 'application/pgp-signature; name="signature.asc"'
    message['Content-Description'] = 'OpenPGP digital signature'
    message.set_payload(signature)
    return message

Then I add all the needed headers to the message (msg) and send it.

This works well for non-multipart messages, but fails when basemsg is multipart (multipart/alternative or multipart/mixed).

Manually verifying the signature against the corresponding piece of text works, but Evolution and Mutt report that the signature is bad.

Can anybody please point me to my mistake?

解决方案

The problem is that Python's email.generator module doesn't add a newline before the signature part. I've reported that upstream as http://bugs.python.org/issue14983.

(The bug was fixed in Python2.7 and 3.3+ in 2014)

这篇关于使用Python进行PGP签名的多部分电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆