.NET 3.5 - Export X509Certificate2 PublicKey - Cannot find the requested object

I am trying to export the X509Certificate2 certificate public key using the following code:

 X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); certificateStore.Open(OpenFlags.ReadOnly); var exportCertificates = certificateStore.Certificates.Find(X509FindType.FindByThumbprint, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", false); certificateStore.Close(); // Get Base64 string of the public key byte[] arr = exportCertificates[0].PublicKey.EncodedKeyValue.RawData; string b64ExportCertificate = Convert.ToBase64String(arr); // Import the certificate X509Certificate2 importCertificate = new X509Certificate2(Convert.FromBase64String(b64ExportCertificate)); 

When I execute the last line, the following exception is thrown:

 System.Security.Cryptography.CryptographicException Cannot find the requested object 

Does anyone know how to solve this?

NOTE. The sample code above is "functional", but this is psuedo code. In fact, I export the certificate to one application, and then transfer it to another for the purpose of digital signatures (from here only sending the public key)

+4
source share
1 answer

Answering my own question:

The problem is the following line (from the example above):

 byte[] arr = exportCertificates[0].PublicKey.EncodedKeyValue.RawData; 

It should be:

 byte[] arr = exportCertificates[0].RawData; 

This may seem contradictory because it “seems” that it will include the entire certificate, not just the public key. However, this is not the case, and this update works as needed.

+8
source

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


All Articles