简体   繁体   English

无法使用MimeKit解密p7m

[英]Unable to decrypt p7m using MimeKit

I have located my smime.p7m from my email message, I read it as stream and try to decrypt it using MimeKit, but it failed with Operation is not valid due to the current state of the object. 我从我的电子邮件中找到了我的smime.p7m ,我将其作为流读取并尝试使用MimeKit对其进行解密,但Operation is not valid due to the current state of the object. ,因此失败Operation is not valid due to the current state of the object.

using (MemoryStream ms = new MemoryStream(data)) {
    CryptographyContext.Register(typeof(WindowsSecureMimeContext));
    ApplicationPkcs7Mime p7m = new ApplicationPkcs7Mime(SecureMimeType.EnvelopedData, ms);
    var ctx = new WindowsSecureMimeContext(StoreLocation.CurrentUser);
    p7m.Verify(ctx, out MimeEntity output);
}

Following the example on https://github.com/jstedfast/MimeKit doesn't help either. 按照https://github.com/jstedfast/MimeKit上的示例也没有帮助。 Anyone familiar with MimeKit could chime in? 任何熟悉MimeKit的人都可以加入?

EDIT: 编辑:

After decrypting the p7m, am I supposed to use the MimeParser to parse the content? 在解密p7m之后,我应该使用MimeParser来解析内容吗? I got the following from the decryption: 我从解密中得到以下内容:

Content-Type: application/x-pkcs7-mime; name=smime.p7m; smime-type=signed-data
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=smime.p7m

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAaCAJIAEWUNvbnRl
bnQtVHlwZTogdGV4dC9wbGFpbjsNCgljaGFyc2V0PSJ1cy1hc2NpaSINCkNvbnRlbnQtVHJhbnNm
ZXItRW5jb2Rpbmc6IDdiaXQNCg0KdGVzdA0KAAAAAAAAoIImTTCCBaIwggOKoAMCAQICBguC3JQz
...more...

But when parsing with MimeParser , 但是在使用MimeParser解析时,

System.FormatException: Failed to parse message headers.
   at MimeKit.MimeParser.ParseMessage(Byte* inbuf, CancellationToken cancellationToken)
   at MimeKit.MimeParser.ParseMessage(CancellationToken cancellationToken)

UPDATE: 更新:

Ah, so it turns, calling Decrypt only gives me the SignedData , I need to then call Verify to pull the original data... this is kind of misleading, I thought Verify would simply verify it... which is why I didn't bother calling it, since I don't really need to verify it... Perhaps it should be call Decode instead? 啊,所以它转过来,调用Decrypt只给我SignedData ,然后我需要调用Verify来提取原始数据......这有点误导,我以为Verify会简单验证它......这就是为什么我没有'打扰它,因为我真的不需要验证它...也许应该调用Decode代替? That's what I was trying to do originally, ((MimePart) signedData).Content.DecodeTo(...) . 这就是我原本想做的事情, ((MimePart) signedData).Content.DecodeTo(...)

So in the end, I had to do something like this to extract the data. 所以最后,我必须做这样的事情来提取数据。

CryptographyContext.Register(typeof(WindowsSecureMimeContext));
ApplicationPkcs7Mime p7m = new ApplicationPkcs7Mime(SecureMimeType.EnvelopedData, ms);
var ctx = new WindowsSecureMimeContext(StoreLocation.CurrentUser);

if (p7m != null && p7m.SecureMimeType == SecureMimeType.EnvelopedData)
{
    // the top-level MIME part of the message is encrypted using S/MIME
    p7m = p7m.Decrypt() as ApplicationPkcs7Mime;
}


if (p7m != null && p7m.SecureMimeType == SecureMimeType.SignedData)
{
    p7m.Verify(out MimeEntity original);    // THE REAL DECRYPTED DATA
    using (MemoryStream dump = new MemoryStream())
    {
        original.WriteTo(dump);
        decrypted = dump.GetBuffer();
    }
}

You are getting an InvalidOperationException because you are calling Verify() on a EncryptedData. 您收到InvalidOperationException,因为您在EncryptedData上调用Verify()。

You need to call Decrypt(). 你需要调用Decrypt()。

Verify() is for SignedData. Verify()适用于SignedData。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM