【问题标题】:Getting exception on server when using RSA via CSharp-easy-RSA-PEM通过 CSharp-easy-RSA-PEM 使用 RSA 时服务器出现异常
【发布时间】:2017-08-04 10:00:16
【问题描述】:

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

它在 IIS 和 Visual Studio 中的本地机器上运行良好,但是当我在服务器上部署我的应用程序时,它给了我以下异常。

"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

经过大量调试,我发现系统没有创建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: 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) 谁能帮忙...

【问题讨论】:

    标签: c# .net asp.net-mvc encryption rsa


    【解决方案1】:

    @Downvoters,请注意。

    我找到了解决这个问题的方法。

    此问题主要是由于 Windows Server 2008 及以后版本中包含的新安全限制所致。

    在 windows server 2008 中,将默认创建一个名为 CryptoGraphic Operator 的新用户。

    如果您的应用程序正在使用 RSACryptoServiceProvider,并且当您决定在 windows server 2008 IIS7 上托管您的应用程序时,请按照以下步骤操作

    1. 应将运行您创建的虚拟目录的相应应用程序池的帐户添加到 CryptoGraphic Operator 用户中。
    2. 打开 IIS7 --> ApplicationPools --> YourAppPool -->RighClikck --> Advanced Settings ---> Load User Profile 将此值设置为 true。

    这解决了我的问题。

    参考:-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】:

      我通过更改流程解决了这个问题,如下所示,无需操作 Web 服务器(IIS、NGINX 或任何其他)。我也在linux上测试过,效果很好。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-15
        • 2019-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多