我已经从我的电子邮件消息中找到了我的smime.p7m,我以流的形式读取它,并尝试使用MimeKit解密它,但使用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的人可以加入进来吗?
编辑:
在解密p7m之后,我是否应该使用MimeParser来解析内容?我从解密中得到了以下信息:
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(...)。
所以最后,我不得不做这样的事情来提取数据。
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();
}
}发布于 2017-12-20 07:38:04
由于您在EncryptedData上调用Verify(),因此您将获得一个InvalidOperationException。
您需要调用Decrypt()。
验证()是否用于SignedData。
https://stackoverflow.com/questions/47894845
复制相似问题