Cannot find the requested object exception when creating X509Certificate2 from a string

I am trying to create X509Certificate2from a string. Let me show you an example:

string keyBase64String = Convert.ToBase64String(file.PKCS7);
var cert = new X509Certificate2(Convert.FromBase64String(keyBase64String));

and keyBase64Stringhas the following content:"MIIF0QYJKoZI ........hvcNAQcCoIIFwjCCBb4CA0="

and file.PKCS7- this is a byte array that I loaded from the database.

I have the following exception when creating X509Certificate2:

Unable to find the requested object

And the stack trace:

"Cannot find the requested object" X509Certificate2 Exception "Cannot find the requested object"} in System.Security.Cryptography.CryptographicException.ThrowCryptographicException (Int32 h) in System.Security.Cryptography.X509Certificates.X509Utils._QueryCertBlobType (bytes) System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob (byte [] rawData, password of the object, X509KeyStorageFlags keyStorageFlags) on System.Security.Cryptography.X509Certificates.X509Certificate2..ctor.D__. [Byte] () in D: \ Projects \ WebApp \ Controllers \ SoupController.cs: line 118

Please tell me what I am doing wrong. Any help would be greatly appreciated!

+4
2

file.PKCS7 PKCS # 7 SignedData ( X509Certificate2 (Collection).Export(X509ContentType.Pkcs7)), :

  • new X509Certificate2(byte[])/new X509Certificate2(string)
    • SignedData. , , , Cannot find the original signer. (Win 2012r2, ).
  • X509Certificate2Collection::Import(byte[])/X509Certificate2Collection::Import(string)
    • "" , .

, PKCS # 7, , , Import (instance) . , //.

+2

X509Certificate2 , (X509Certificate2 Constructor (String))

, keyBase64String , . :

var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates.Find(X509FindType.FindByThumbprint, keyBase64String , false);
//var certCollection = store.Certificates.Find(X509FindType.FindByKeyUsage, keyBase64String , false);
//var certCollection = store.Certificates.Find(X509FindType.FindBySubjectKeyIdentifier, keyBase64String , false);
var cert = certCollection[0];

FindByKeyUsage, FindBySubjectKeyIdentifier X509FindType Enumeration

+1

Source: https://habr.com/ru/post/1677480/


All Articles