0

I have a issue loading x509 certificate in net.core. I use the following code :

var bytes = File.ReadAllBytes(certificatePath);
var cert = new X509Certificate2(bytes);

and I get the following error :

Cannot find the requested object

If I change the code to :

var cert = new X509Certificate2();
cert.Import(_path);

I get thrown the following exception:

System.PlatformNotSupportedException : X509Certificate is immutable on this 
platform. Use the equivalent constructor instead.

Other useful infos:

  • Certificate_path": "C:\XYZ\Documents\XYZ\rsakey.txt
  • Certificate and alghoritm info : RSA encryption strategy with mode ECB, padding PKCS1Padding and KeyLength 1024 is used to encrypt data at client side.

I discovered that I'm trying to pass a public key instead of a certificate but I don't know how can I generate certificate in c# from a public key. How can I solve this problem? Thanks

  • What is your certificatePath value ? What certificate extension you are trying to import ? – Bartosz Olchowik Jul 27 '22 at 06:44
  • @BartoszOlchowik I completed the post with the required info. – Simone Spagna Jul 27 '22 at 07:04
  • imo you should create an .pfx file from your cert and key, and then try to create `X509Certificate2` by giving path to .pfx file. You might find something usefull about that process under this link: https://stackoverflow.com/questions/6307886/how-to-create-pfx-file-from-certificate-and-private-key – Bartosz Olchowik Jul 27 '22 at 08:41
  • Does rsakey.txt say something inside like `-----BEGIN CERTIFICATE-----`? Or is it perhaps instead `-----BEGIN PRIVATE KEY-----` or `-----BEGIN RSA PRIVATE KEY-----`? The latter two, for example, aren't valid as certificates. – bartonjs Jul 27 '22 at 16:03
  • @bartonjs you're right. It isn't a certificate but a public key. How can I generate certificate from a public key? – Simone Spagna Jul 28 '22 at 08:13

0 Answers0