下面的代码在 .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
提示密码不对,可能需要再linux系统上重新生成一个pfx
传入密码也是同样的报错,.NET 3.1 中不需要密码也能读取
的确是密码不对
为啥我升级为5.0后出现另一个错误,可以指教下吗
Interop+Crypto+OpenSslCryptographicException: error:2006D080:BIO routines:BIO_new_file:no such file
这个问题我今天也遇到了
https://github.com/dotnet/runtime/issues/44535
按照帖子里面生成证书的时候删掉 -certfile [xxxx].crt 就可以了