通过CSharp-easy-RSA-PEM使用RSA时在服务器上获取异常 [英] Getting exception on server when using RSA via CSharp-easy-RSA-PEM

查看:52
本文介绍了通过CSharp-easy-RSA-PEM使用RSA时在服务器上获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 https://github.com/jrnker/CSharp-easy-RSA-PEM 用于我的mvc项目中的RSA实现.

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

在我在IIS&中的本地计算机中运行正常也可以通过Visual Studio,但是当我在服务器上部署我的应用程序时,它给了我下面的例外.

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)"

我的代码是:

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;
    }
}

我也在github上发布了我的问题@ https://github.com/jrnker/CSharp-easy-RSA-PEM/issues/8

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

经过大量调试后,我发现系统没有创建 RSACryptoServiceProvider

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();

异常:- System.Security.Cryptography.CryptographicException:系统找不到指定的文件.\ r \ n \ r \ n位于CoreEntities.Classes.Utility.RSADecrypt(字符串输入)\ r \ n位于WebAPI.Attributes.ApiAuthorizeAttribute.Authorize(HttpActionContext actionContext)谁能帮忙...

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

推荐答案

@Downvoters,请注意.

@Downvoters, Kindly pay attention.

我找到了解决此问题的方法.

I found solution to this problem.

此问题主要是由于Windows Server 2008及更高版本中包含的新安全约束.

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

在Windows Server 2008中,默认情况下将创建一个名为CryptoGraphic Operator的新用户.

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

如果您的应用程序使用RSACryptoServiceProvider,并且当您决定将应用程序托管在Windows Server 2008 IIS7上时,请执行以下步骤

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

  1. 您创建的虚拟目录的相应应用程序池所运行的帐户应添加到CryptoGraphic Operator用户中.
  2. 打开IIS7-> ApplicationPools-> YourAppPool-> RighClikck->高级设置--->加载用户配置文件"将此值设置为true.

这解决了我的问题.

Ref:- 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆