H5W3
当前位置:H5W3 > 问答 > 正文

.NET 5.0 在 Linux 上读取 pfx 证书的问题

下面的代码在 .net core 3.1 中可以在 linux 上正常读取 pfx 证书,升级到 .net 5.0 之后却无法在 linux 上读取 pfx 证书(在 windows 上没问题)

public static class CertificateFactory
{
public static X509Certificate2 DefaultCertificate
{
get
{
var assembly = typeof(CertificateFactory).GetTypeInfo().Assembly;
using (var stream = assembly.GetManifestResourceStream(
assembly.GetManifestResourceNames().First(r => r.EndsWith("cnblogs.pfx"))))
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
return new X509Certificate2(bytes);
}
}
}
}

报错信息如下

289   System.Security.Cryptography.CryptographicException : The certificate data cannot be read with the provided password, the password may be incorrect.
290---- System.Security.Cryptography.CryptographicException : The certificate data cannot be read with the provided password, the password may be incorrect.
291  Stack Trace:
292     at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(SafePasswordHandle password)
293   at Internal.Cryptography.Pal.PkcsFormatReader.TryReadPkcs12(OpenSslPkcs12Reader pfx, SafePasswordHandle password, Boolean single, ICertificatePal& readPal, List`1& readCerts)
294   at Internal.Cryptography.Pal.PkcsFormatReader.TryReadPkcs12(ReadOnlySpan`1 rawData, SafePasswordHandle password, Boolean single, ICertificatePal& readPal, List`1& readCerts, Exception& openSslException)
295   at Internal.Cryptography.Pal.OpenSslX509CertificateReader.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
296   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)
297   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)

请问如何解决?

回答

问题补充:

github 上的相关 issue :Linux and Mac have CryptographicException with generic HResult for bad password opening a PFX

未经允许不得转载:H5W3 » .NET 5.0 在 Linux 上读取 pfx 证书的问题

赞 (0)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址