'Cannot find the requested object' Error loading *private key* into X509Certificate2 in .Net Core

0

We're using the System.Security.Cryptography.X509Certificates component in a .Net Core application and running into this error trying to load a private key. The only functionality that we need for the certificate is to decrypt a string and check the NotAfter value. The line of code that errors out is:

var cert = new X509Certificate2(privateKeyByteArr);

The exception:

    Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException
  HResult=0x80092009
  Message=Cannot find the requested object
 Source=System.Security.Cryptography.X509Certificates  Cannot find the requested object
  StackTrace:
   at Internal.Cryptography.Pal.CertificatePal.FromBlobOrFile(Byte[] rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] data)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData)
   at Telematics.CommonUtilities.Services.RSACryptoService.DecryptKey(String key, Byte[] privateKeyByteArr) in D:\Users\I25266\Source\Repos\common utils feature b\telematics.commonutilities\Telematics.CommonUtilities\Services\RSACryptoService.cs:line 383

The same approach works fine with a public key, and this same private key can be used successfully with BouncyCastle. I've also tried the X509Certificate2Collection Import() method - same error. Even though we have a work-around, I'd like to use the native .Net components.

Any suggestion or explanation would be very welcome.

.net-core
x509certificate2
asked on Stack Overflow Mar 15, 2019 by Reuven Trabin • edited Mar 15, 2019 by Reuven Trabin

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0