0
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?

2
  • 2
    Please edit your post to include the full complete error message you get. Also add a minimal reproducible example, which produce the error. Also add the .csproj file, so we see which packages you have installed. Commented Sep 13 at 11:07
  • 1
    There is no "file" in your code snippet, so it's unlikely the error is coming from it. In addition to the necessary advice in the previous comment, run the debugging version of your code so that the complete error message contains source code line numbers to pinpoint the problem line. Commented Sep 13 at 12:53

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.