Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find the requested object. when create X509certificate2

I'm trying to create X509Certificate2 with private_key string using the following code:

 byte[] key = Convert.FromBase64String(private_key);
 X509Certificate2 certificate = new X509Certificate2(key);

When the last line executes the following exception is thrown:

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

Does anybody know how to resolve this?

like image 674
nam do Avatar asked Nov 08 '22 12:11

nam do


1 Answers

A private key doesn't typically be in the form of an X.509 certificate. You can either find it as PFX or P12 files which you should import into a X509CertificateCollection to get the private key as an X509Certificate instance.

The other alternative (if the private key you have is in the form of a PFX or P12 file) is to load the private key into the windows local certificate store and then load it using the X509Store class which would give you the private key as X509Certificate instance.

Since you have a PEM file, then see this Convert a CERT/PEM certificate to a PFX certificate for how to convert it into a PFX file.

like image 122
Timothy Ghanem Avatar answered Nov 15 '22 07:11

Timothy Ghanem