Hi, unfortunately I wasn't able to find any resources on this. I'm unable to properly access some of the Cryptography libraries in my project. My code looks like
.
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
byte[] publicPemBytes = Encoding.UTF8.GetBytes(publicCert);
using var publicX509 = new X509Certificate2(publicPemBytes);
var privateKeyBlocks = privateCert.Split("-",
StringSplitOptions.RemoveEmptyEntries);
var privateKeyBytes = Convert.FromBase64String(privateKeyBlocks[1]);
RSA rsa = RSA.Create();
if (privateKeyBlocks[0] == "BEGIN PRIVATE KEY")
{
rsa.ImportPkcs8PrivateKey(privateKeyBytes, out _);
}
else if (privateKeyBlocks[0] == "BEGIN RSA PRIVATE KEY")
{
rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
}
X509Certificate2 keyPair = publicX509.CopyWithPrivateKey(rsa);
return keyPair;
However I am getting a number of unexpected errors like "'X509Certificate2' does not contain a definition for 'CopyWithPrivateKey' and no accessible extension method 'CopyWithPrivateKey' accepting a first argument of type 'X509Certificate2' could be found" and "'RSA' does not contain a definition for 'ImportRSAPrivateKey' and no accessible extension method 'ImportRSAPrivateKey' accepting a first argument of type 'RSA' could be found" and "'RSA' does not contain a definition for 'ImportPkcs8PrivateKey' and no accessible extension method 'ImportPkcs8PrivateKey' accepting a first argument of type 'RSA' could be found "
I am not sure why these errors are occurring because I'm pretty sure I have all the correct imports. When I try to run this is a different project, I don't get these errors. Only when it's in the project I actually need it in. Is there a reason for this or something I'm doing wrong?