Warm tip: This article is reproduced from serverfault.com, please click

Getting exception on server when using RSA via CSharp-easy-RSA-PEM

发布于 2017-08-04 10:00:16

I have used https://github.com/jrnker/CSharp-easy-RSA-PEM for RSA implementation in my mvc project.

It's working fine in my local machine in IIS & also via visual studio but when I deploy my application on server, it gives me below exception.

"System.NullReferenceException: Object reference not set to an instance of an object.\r\n at CoreEntities.Classes.Utility.RSADecrypt(String input)\r\n at WebAPI.Attributes.ApiAuthorizeAttribute.Authorize(HttpActionContext actionContext)"

My code is :

public static string RSADecrypt(string input)
{
    try
    {
        string priv = File.ReadAllText(HostingEnvironment.MapPath("~/Certificates/consumer.pem"));
        RSACryptoServiceProvider privc = Crypto.DecodeRsaPrivateKey(priv);
        return Crypto.DecryptString(input, privc);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

I posted my issue on github also @ https://github.com/jrnker/CSharp-easy-RSA-PEM/issues/8

After debugging a lot, I figured out that system is not creating an object of RSACryptoServiceProvider

CspParameters parms = new CspParameters();
parms.Flags = CspProviderFlags.NoFlags;
parms.KeyContainerName = Guid.NewGuid().ToString().ToUpperInvariant();
parms.ProviderType = ((Environment.OSVersion.Version.Major > 5) || ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor >= 1))) ? 0x18 : 1;

// Exception is comping in below line.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(parms);

RSAParameters rsAparams = new RSAParameters();

Exception:- System.Security.Cryptography.CryptographicException: The system cannot find the file specified.\r\n\r\n at CoreEntities.Classes.Utility.RSADecrypt(String input)\r\n at WebAPI.Attributes.ApiAuthorizeAttribute.Authorize(HttpActionContext actionContext) Can anyone please help...

Questioner
Jitendra Pancholi
Viewed
0
Jitendra Pancholi 2017-08-09 13:50:53

@Downvoters, Kindly pay attention.

I found solution to this problem.

This problem is mainly due to the new security constraints that were included into windows server 2008 onwards.

In windows server 2008 a new user with name CryptoGraphic Operator will be created by default.

If your application is using RSACryptoServiceProvider and when you decide to host your application on windows server 2008 IIS7 follow below steps

  1. the account under which the respective application pool of the virtual directory that you create is running should be added in to CryptoGraphic Operator user.
  2. Open IIS7 --> ApplicationPools --> YourAppPool -->RighClikck --> Advanced Settings ---> Load User Profile set this value to true.

This solved my problem.

Ref:- https://social.msdn.microsoft.com/Forums/vstudio/en-US/ec93922a-fd1e-4225-b5cf-1472ebb3acd1/systemsecuritycryptographycryptographicexception-the-system-cannot-find-the-file-specified?forum=netfxbcl