M2Crypto:尝试解密SMIME字符串。获取无内容类型错误

时间:2018-05-13 23:59:38

标签: python encryption m2crypto smime

我尝试解密SMIME加密邮件(使用PKI.js库在JavaScript中加密),如下所示:

Content-Type: application/pkcs7-mime; name=smime.p7m;
 smime-type=enveloped-data
Content-Description: Enveloped Data
Content-Disposition: attachment; filename=smime.p7m
Content-Transfer-Encoding: base64
From: sender@example.com
To: recipient@example.com
Subject: Example S/MIME encrypted message
Date: Sun, 13 May 2018 23:53:48 +0000
Message-Id: <1526255628125-055f663b-fd554ae9-1ed33cf1@example.com>
MIME-Version: 1.0

MIIBzAYJKoZIhvcNAQcDoIIBvTCCAbkCAQIxggFuMIIBagIBADAjMB4xHDAJBgNVBAYTAlJVMA8G
A1UEAx4IAFQAZQBzAHQCAQEwPAYJKoZIhvcNAQEHMC+gDzANBglghkgBZQMEAgMFAKEcMBoGCSqG
SIb3DQEBCDANBglghkgBZQMEAgMFAASCAQA6EIOlgaYQ1/wYJe4DfZ5EDMLvCFbkj1h3CPtCsw7A
dKDdQAO1Sn25T0yyAO5iaXkETUGX2DGEqzOFspROrXObiuk4vLu0/D78780FhU59ybLY41m4ydT3
Dxpz3+2e3aRgfP2qnwG0WepIe14OWQWTZwtvRRrsh3AhjE9NAQik1MicPA8mTlQYKge6FEFbRvzu
+PNXht9bn/a+asSrPJleesST+srGm6/lllmTMOtqcGp5aelkQwCQ/aHJJ9HTSYtg8TAb4N78o00C
YJrhMrsKygJSC65VGV2BDbsbyBIhuiIdrCWx3JNoaKd2N6uix8V0UCHZHkzXnEduL5RH9SWdMIAG
CSqGSIb3DQEHATAdBglghkgBZQMEAQIEEGtZdxyVU9+8aONDzGlUjFuggAQQECObjBYnr23zUJ+6
60kRvAAAAAA=

我这样做的方式如下:

from M2Crypto import BIO, SMIME, X509

def decrypt(s):
    mime = SMIME.SMIME()

    with open("tmp.p7", 'wb') as f:
        f.write(s.encode('utf-8'))
        p7, data = SMIME.smime_load_pkcs7("tmp.p7")
    return mime.decrypt(p7)

我得到的是以下信息:

File "/Users/Adam/Library/Python/3.5/lib/python/site-packages/M2Crypto/SMIME.py", line 113, in smime_load_pkcs7
    p7_ptr, bio_ptr = m2.smime_read_pkcs7(bio)
M2Crypto.SMIME.SMIME_Error: no content type

如果我这样做

with open("tmp.p7", 'w') as f:
            f.write(s)

相反,结果是相同的...没有内容类型错误消息。

请有人帮助我吗?

1 个答案:

答案 0 :(得分:0)

您正在查看的是完整邮件,而不仅仅是 CMS或PKCS#7内容

剥离邮件标题,直到您只剩下基本64位编码(在换行符后面开始),然后重试。

您可以看到自己的数据here (online JavaScript ASN.1 decoder)

这些是非常新的算法(带有MGF1 / SHA-512和AES的RSA-OAEP),但最后它们只加密了一个16字节的密文,这确实没有以任何方式识别。因此,在这种意义上,内容类型 缺失,也许您可​​以以某种方式将其作为参数。

不会发布评论,因为它不适合,您的数据位于网址中。