MimeKit - 需要帮助加载 pfx 文件:NullReferenceException
MimeKit - Need help loading pfx file: NullReferenceException
我是 C# 和 mimekit 的新手,正在尝试根据 MimeKits github 自述文件进行基本 S/MIME 设置。正在尝试加载 pfx 文件以解密 S/MIME 条消息。我的代码:
public void LoadFile(string filename)
{
this.Message = MimeMessage.Load(filename);
Console.WriteLine($"Loaded {filename}.");
CryptographyContext.Register(typeof(MySecureMimeContext));
using (var ctx = new MySecureMimeContext())
{
using (var stream = File.OpenRead("/path/to/keyStore.p12"))
{
ctx.Import(stream, "testas2");
}
}
}
这是安全的 MIME 上下文文件:
using System.IO;
using System.Data.SQLite;
using MimeKit.Cryptography;
namespace dotnet_smime
{
class MySecureMimeContext : DefaultSecureMimeContext
{
public MySecureMimeContext() : base(OpenDatabase("certdb.sqlite"))
{
}
static IX509CertificateDatabase OpenDatabase(string fileName)
{
var builder = new SQLiteConnectionStringBuilder();
builder.DateTimeFormat = SQLiteDateFormats.Ticks;
builder.DataSource = fileName;
if (!File.Exists(fileName))
SQLiteConnection.CreateFile(fileName);
var sqlite = new SQLiteConnection(builder.ConnectionString);
sqlite.Open();
return new SqliteCertificateDatabase(sqlite, "password");
}
}
}
当代码调用 ctx.Import(...) 时,我得到:
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at MimeKit.Cryptography.BouncyCastleCertificateExtensions.AsHex(Byte[] blob)
at MimeKit.Cryptography.X509CertificateDatabase.GetValue(X509CertificateRecord record, String columnName)
at MimeKit.Cryptography.SqlCertificateDatabase.GetInsertCommand(X509CertificateRecord record)
at MimeKit.Cryptography.X509CertificateDatabase.Add(X509CertificateRecord record)
at MimeKit.Cryptography.DefaultSecureMimeContext.Import(Stream stream, String password)
at dotnet_smime.MimePkcs7.LoadFile(String filename) in /Users/sakwaalvitre/projects/edi/dotnet-smime/MimePkcs7.cs:line 27
at dotnet_smime.Program.Main(String[] args) in /Users/sakwaalvitre/projects/edi/dotnet-smime/Program.cs:line 19
据我所知,ctx 和流都已定义并且流有数据。
提前致谢!
这是 MimeKit 2.5.2 中的一个错误,当证书的 SubjectKeyIdentifier 为空时会发生该错误。
试用 https://www.myget.org/feed/mimekit/package/nuget/MimeKit
中的 nuget
我刚刚做了一个修复,2.5.2.16 版本应该包含这个修复。
如果不起作用,请告诉我。
我是 C# 和 mimekit 的新手,正在尝试根据 MimeKits github 自述文件进行基本 S/MIME 设置。正在尝试加载 pfx 文件以解密 S/MIME 条消息。我的代码:
public void LoadFile(string filename)
{
this.Message = MimeMessage.Load(filename);
Console.WriteLine($"Loaded {filename}.");
CryptographyContext.Register(typeof(MySecureMimeContext));
using (var ctx = new MySecureMimeContext())
{
using (var stream = File.OpenRead("/path/to/keyStore.p12"))
{
ctx.Import(stream, "testas2");
}
}
}
这是安全的 MIME 上下文文件:
using System.IO;
using System.Data.SQLite;
using MimeKit.Cryptography;
namespace dotnet_smime
{
class MySecureMimeContext : DefaultSecureMimeContext
{
public MySecureMimeContext() : base(OpenDatabase("certdb.sqlite"))
{
}
static IX509CertificateDatabase OpenDatabase(string fileName)
{
var builder = new SQLiteConnectionStringBuilder();
builder.DateTimeFormat = SQLiteDateFormats.Ticks;
builder.DataSource = fileName;
if (!File.Exists(fileName))
SQLiteConnection.CreateFile(fileName);
var sqlite = new SQLiteConnection(builder.ConnectionString);
sqlite.Open();
return new SqliteCertificateDatabase(sqlite, "password");
}
}
}
当代码调用 ctx.Import(...) 时,我得到:
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at MimeKit.Cryptography.BouncyCastleCertificateExtensions.AsHex(Byte[] blob)
at MimeKit.Cryptography.X509CertificateDatabase.GetValue(X509CertificateRecord record, String columnName)
at MimeKit.Cryptography.SqlCertificateDatabase.GetInsertCommand(X509CertificateRecord record)
at MimeKit.Cryptography.X509CertificateDatabase.Add(X509CertificateRecord record)
at MimeKit.Cryptography.DefaultSecureMimeContext.Import(Stream stream, String password)
at dotnet_smime.MimePkcs7.LoadFile(String filename) in /Users/sakwaalvitre/projects/edi/dotnet-smime/MimePkcs7.cs:line 27
at dotnet_smime.Program.Main(String[] args) in /Users/sakwaalvitre/projects/edi/dotnet-smime/Program.cs:line 19
据我所知,ctx 和流都已定义并且流有数据。
提前致谢!
这是 MimeKit 2.5.2 中的一个错误,当证书的 SubjectKeyIdentifier 为空时会发生该错误。
试用 https://www.myget.org/feed/mimekit/package/nuget/MimeKit
中的 nuget我刚刚做了一个修复,2.5.2.16 版本应该包含这个修复。
如果不起作用,请告诉我。