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

亡梦爱人 提交于 2021-01-02 05:02:34

问题


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...


回答1:


@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




回答2:


I solved the problem by changing the process Like below and no need to maniupulate the web server (IIS, NGINX or any other). I also tested that on linux, works fine.

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
string toBeSigned= "string";
byte[] signMain = rsa.SignData(Encoding.UTF8.GetBytes(toBeSigned), new SHA1CryptoServiceProvider());
string signature = Convert.ToBase64String(signMain);


来源:https://stackoverflow.com/questions/45503830/getting-exception-on-server-when-using-rsa-via-csharp-easy-rsa-pem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!