无法使用MimeKit解密p7m [英] Unable to decrypt p7m using MimeKit

查看:134
本文介绍了无法使用MimeKit解密p7m的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从我的电子邮件中找到了smime.p7m,我将其读取为流,并尝试使用MimeKit对其进行解密,但是由于Operation is not valid due to the current state of the object.

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.

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);
}

遵循 https://github.com/jstedfast/MimeKit 上的示例无济于事任何一个.任何熟悉MimeKit的人都能听到提示音吗?

Following the example on https://github.com/jstedfast/MimeKit doesn't help either. Anyone familiar with MimeKit could chime in?

解密p7m之后,我应该使用MimeParser解析内容吗?我从解密中得到了以下信息:

After decrypting the p7m, am I supposed to use the MimeParser to parse the content? 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...

但是使用MimeParser解析时,

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

更新:

嗯,事实如此,调用Decrypt仅给了我SignedData,然后我需要调用Verify来提取原始数据...这有点误导,我以为Verify会简单地对其进行验证...这就是为什么我不打扰它的原因,因为我真的不需要验证它.也许应该改为调用Decode?那就是我最初试图做的,((MimePart) signedData).Content.DecodeTo(...).

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? That's what I was trying to do originally, ((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();
    }
}

推荐答案

由于在EncryptedData上调用Verify(),因此您收到了InvalidOperationException.

You are getting an InvalidOperationException because you are calling Verify() on a EncryptedData.

您需要调用Decrypt().

You need to call Decrypt().

Verify()用于SignedData.

Verify() is for SignedData.

这篇关于无法使用MimeKit解密p7m的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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