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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With