MimeKit-无法将'Org.BouncyCastle.Asn1.DerApplicationSpecific'类型的对象强制转换为'Org.BouncyCastle.Asn1.Asn1SequenceParser'类型 [英] Mimekit - Unable to cast object of type 'Org.BouncyCastle.Asn1.DerApplicationSpecific' to type 'Org.BouncyCastle.Asn1.Asn1SequenceParser'

查看:0
本文介绍了MimeKit-无法将'Org.BouncyCastle.Asn1.DerApplicationSpecific'类型的对象强制转换为'Org.BouncyCastle.Asn1.Asn1SequenceParser'类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MIMEKIT加密和解密MIME邮件,每次尝试解密邮件时都会遇到此错误:

意外对象正在读取内容。BouncyCastle.Crypto at Org.BouncyCastle.Cms.CmsContentInfoParser..ctor(Stream Data)在//crypto/src/cms/CMSContentInfoParser.cs:line 35中 At Org.BouncyCastle.Cms.CmsEnvelopedDataParser..ctor(Stream Entained Data)在//crypto/src/cms/CMSEnvelopedDataParser.cs:line 65中 在MimeKit.Cryptography.BouncyCastleSecureMimeContext.d__50.MoveNext() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务中) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务中) 在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() At PasarelaLibrary.Bases.GraphService.BaseGraphPasarela.d__11.MoveNext()in C:DevEurovalPasarelaAceuroPasarelaLibraryBasesGraphServiceBaseGraphPasarela.cs:line 302 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务中) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务中) 在System.Runtime.CompilerServices.TaskAwaiter.GetResult() At PasarelaLibrary.Bases.GraphService.BaseGraphPasarela.d__9.MoveNext()in C:DevEurovalPasarelaAceuroPasarelaLibraryBasesGraphServiceBaseGraphPasarela.cs:line 237

内部异常

无法将类型为‘Org.BouncyCastle.Asn1.DerApplicationSpecific’的对象强制转换为类型‘Org.BouncyCastle.Asn1.Asn1SequenceParser’。At Org.BouncyCastle.Cms.CmsContentInfoParser..ctor(Stream Data)在/_/crypto/src/cms/CMSContentInfoParser.cs:line 27中

问题是,我只是试图加密和解密一条消息,以测试库和应用程序流,但我收到了这个错误。在上面你可以找到我正在使用的代码。我使用的x509证书带有要在TemporarySecureMimeContext中导入的密码。

using var context = new TemporarySecureMimeContext();
await context.ImportAsync(certificate);
var encryptedMessage = await GetEncryptedMessage(context, stream, fileroute, certificate, mailFrom, mailTo);
using var testencrypted = new MemoryStream();
await encryptedMessage.WriteToAsync(testencrypted);
testencrypted.Position = 0;
var dec = await context.DecryptAsync(testencrypted); //here it explodes :(

public static async Task<MimeMessage> GetEncryptedMessage(TemporarySecureMimeContext context, Stream stream, string subject, X509Certificate certificate, string mailFrom, string mailTo)
{
    stream.Position = 0;
    SecureMailboxAddress mailFromEncrypted = new SecureMailboxAddress("name", mailFrom, certificate.GetFingerprint());
    SecureMailboxAddress mailToEncrypted = new SecureMailboxAddress("name", mailTo, certificate.GetFingerprint());
    BodyBuilder bodyBuilder = new BodyBuilder();
    using StreamReader reader = new StreamReader(stream);
    bodyBuilder.TextBody = await reader.ReadToEndAsync();
    MimeMessage message = new MimeMessage(new List<InternetAddress> { mailFromEncrypted }, new List<InternetAddress> { mailToEncrypted }, subject, bodyBuilder.ToMessageBody());
    message.Date = DateTime.Now;
    message.MessageId = MimeUtils.GenerateMessageId();
    await message.EncryptAsync(context);
    return message;
}

我已经在这里和其他论坛上阅读了其他帖子,但在这个案例中都没有奏效。有人能帮我一下吗?

推荐答案

您使用错误:-)

您正在尝试解密MIME消息流。你不能那样做。

SecureMimeConext.Decillit()和DecillitAsync()方法需要MIME消息的加密内容

如果您的目标是加载MimeMessage并将其解密,则应将代码更改为:

using var context = new TemporarySecureMimeContext();
await context.ImportAsync(certificate);

// get an encrypted message
var encryptedMessage = await GetEncryptedMessage(context, stream, fileroute, certificate, mailFrom, mailTo);

// write the encrypted message to a stream
using var testencrypted = new MemoryStream();
await encryptedMessage.WriteToAsync(testencrypted);
testencrypted.Position = 0;

// load the message from the stream
var loadedMessage = await MimeMessage.LoadAsync(testencrypted);

// get the encrypted body
var encryptedBody = (ApplicationPkcs7Mime) loadedMessage.Body;

// decrypt it
var decryptedBody = await encryptedBody.DecryptAsync(context);

// restore the message to the pre-encrypted state
loadedMessage.Body = decryptedBody;

这篇关于MimeKit-无法将&#39;Org.BouncyCastle.Asn1.DerApplicationSpecific&#39;类型的对象强制转换为&#39;Org.BouncyCastle.Asn1.Asn1SequenceParser&#39;类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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