base64asp.net-core-webapirsa.net-6.0private-key

rsa.ImportFromPem(privateKeyString) throws strange error in ASP.NET Core 6.0 Web API


using (RSA rsa = RSA.Create()) 
{
    rsa.ImportFromPem(privateKeyString);
    byte[] dataToSign = Encoding.UTF8.GetBytes(signData);
    byte[] signedData = rsa.SignData(dataToSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
    signatureBase64 = Convert.ToBase64String(signedData);
}

This code works fine as a Windows Forms application in .NET 6.0. However, it throws a strange error:

The system cannot find the file specified

in an ASP.NET Core 6.0 Web API.

privateKeyString is of the form

-----BEGIN PRIVATE KEY----- 
..... 
-----END PRIVATE KEY-----

Where is my mistake?


Solution

  • From the comments, I realized maybe it is just outdated library issue, aaand it was. I changed my project from .net 6.0 to .net 8.0 and updated libraries accordingly then the code works fine. Thanks "President James K. Polk" and "Progman" for comments