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'
我正在使用 mimekit 来加密和解密 mime 消息,但每次我尝试解密消息时都会遇到此错误:
Unexpected object reading content. BouncyCastle.Crypto at Org.BouncyCastle.Cms.CmsContentInfoParser..ctor(Stream data) in //crypto/src/cms/CMSContentInfoParser.cs:line 35
at Org.BouncyCastle.Cms.CmsEnvelopedDataParser..ctor(Stream envelopedData) in //crypto/src/cms/CMSEnvelopedDataParser.cs:line 65
at MimeKit.Cryptography.BouncyCastleSecureMimeContext.d__50.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at PasarelaLibrary.Bases.GraphService.BaseGraphPasarela.d__11.MoveNext() in C:\Dev\Euroval\PasarelaAceuro\PasarelaLibrary\Bases\GraphService\BaseGraphPasarela.cs:line 302
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at PasarelaLibrary.Bases.GraphService.BaseGraphPasarela.d__9.MoveNext() in C:\Dev\Euroval\PasarelaAceuro\PasarelaLibrary\Bases\GraphService\BaseGraphPasarela.cs:line 237
内部异常
Unable to cast object of type 'Org.BouncyCastle.Asn1.DerApplicationSpecific' to type 'Org.BouncyCastle.Asn1.Asn1SequenceParser'. at Org.BouncyCastle.Cms.CmsContentInfoParser..ctor(Stream data) in /_/crypto/src/cms/CMSContentInfoParser.cs:line 27
问题是我只是试图加密和解密消息以测试库和应用程序的流程,但我收到了这个错误。在上面你可以找到我正在使用的代码。我正在使用 x509Certificate 和我在 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 消息流。你不能那样做。
SecureMimeContext.Decrypt() 和 DecryptAsync() 方法需要 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 来加密和解密 mime 消息,但每次我尝试解密消息时都会遇到此错误:
Unexpected object reading content. BouncyCastle.Crypto at Org.BouncyCastle.Cms.CmsContentInfoParser..ctor(Stream data) in //crypto/src/cms/CMSContentInfoParser.cs:line 35 at Org.BouncyCastle.Cms.CmsEnvelopedDataParser..ctor(Stream envelopedData) in //crypto/src/cms/CMSEnvelopedDataParser.cs:line 65 at MimeKit.Cryptography.BouncyCastleSecureMimeContext.d__50.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at PasarelaLibrary.Bases.GraphService.BaseGraphPasarela.d__11.MoveNext() in C:\Dev\Euroval\PasarelaAceuro\PasarelaLibrary\Bases\GraphService\BaseGraphPasarela.cs:line 302 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at PasarelaLibrary.Bases.GraphService.BaseGraphPasarela.d__9.MoveNext() in C:\Dev\Euroval\PasarelaAceuro\PasarelaLibrary\Bases\GraphService\BaseGraphPasarela.cs:line 237
内部异常
Unable to cast object of type 'Org.BouncyCastle.Asn1.DerApplicationSpecific' to type 'Org.BouncyCastle.Asn1.Asn1SequenceParser'. at Org.BouncyCastle.Cms.CmsContentInfoParser..ctor(Stream data) in /_/crypto/src/cms/CMSContentInfoParser.cs:line 27
问题是我只是试图加密和解密消息以测试库和应用程序的流程,但我收到了这个错误。在上面你可以找到我正在使用的代码。我正在使用 x509Certificate 和我在 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 消息流。你不能那样做。
SecureMimeContext.Decrypt() 和 DecryptAsync() 方法需要 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;